Data for the current week

  • Hi, I m trying to write a query to find out all the data in the current week. Like, If I run query on any day of the week, I wanted get all the data from Monday 12.01 AM. Can someone help me in this?

    Thanks a lot

  • This is a lot easier if we had some sample schema and test data to show you proven code. See the first link in my signature for help on that.

    Generically: You're looking at using SET DATEFIRST to make sure you start on the right day, DATEPART( dw) for your day of the week, subtracting that from the current date, manipulating the time component of GETDATE() a bit with DATEADD/DATEDIFF, and then using that as a parameter in your WHERE clause.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • This is based on a week from Monday 12:00 AM to Sunday 11:59:59.999PM:

    SELECT

    FROM YourTable

    WHERE YourColumn > = DATEADD(wk,DATEDIFF(wk,0, GETDATE() -1 ),0)



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • @Craig: I strongly vote against any method involving SET DATEFIRST and/or DATEPART(dw)

    One of the (undesired?) side effects: once you have a specific SET DATEFIRST in your batch, any following SET LANGUAGE won't change the DATEFIRST value anymore (but it will without the specific DATEFIRST setting earlier in that batch).

    This might sound like a very specific scenario. But once you got trapped by it, it'll take a while to find and fix it accordingly....



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thanks LutZ. It works perfect

  • LutzM (6/24/2011)


    @Craig: I strongly vote against any method involving SET DATEFIRST and/or DATEPART(dw)

    One of the (undesired?) side effects: once you have a specific SET DATEFIRST in your batch, any following SET LANGUAGE won't change the DATEFIRST value anymore (but it will without the specific DATEFIRST setting earlier in that batch).

    This might sound like a very specific scenario. But once you got trapped by it, it'll take a while to find and fix it accordingly....

    I'm not necessarily sure I'd want SET LANGUAGE to be changing my DATEFIRST once I've specifically set it anyway, so your concern is actually a benefit to me. I'll keep it in mind though.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Craig Farrell (6/24/2011)


    ...

    I'm not necessarily sure I'd want SET LANGUAGE to be changing my DATEFIRST once I've specifically set it anyway, so your concern is actually a benefit to me. I'll keep it in mind though.

    Sounds like a classic "it depends" then 😀



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply