Solved

Get Tuesday through Monday in query

Posted on 2013-10-22
Medium Priority
299 Views
I want to pull data from last week to this week starting on Tuesday of last week through the next Monday (of the current week).
0
Question by:williamss132
• 2

LVL 66

Accepted Solution

Jim Horn earned 2000 total points
ID: 39592233
Here's the code to get the current day of the week (1=Sunday to 7-Saturday)
``````SELECT DATEPART(dw, GETDATE())
``````
So, doing a little math...
``````Declare @dt date = '10-25-13'

SELECT DATEADD(d,  - (DATEPART(dw, @dt) + 2), @dt) as last_thursday,
DATEADD(d,  - (DATEPART(dw, @dt) - 2), @dt) as this_monday
``````
btw Here's an article I wrote on How to build your own SQL Calendar Table that demonstrates lots of goofy-riffic date expressions you can use.
0

LVL 9

Expert Comment

ID: 39592281
Here is how you can determine the most recent Tuesday:
``````DECLARE @myTuesdayDate date, @myMondayDate date;
SELECT @myTuesdayDate = DATEADD(DAY, DATEDIFF(DAY, 2, GETDATE()) / 7 * 7, 1);
``````
Assign this to a date variable, then do a
``````SELECT @myMondayDate = DATEADD(day, 6, @myTuesdayDate)
``````
and you get your next Monday.  Then use these values as a range for your select
0

LVL 66

Expert Comment

ID: 39611684
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then readingâ€¦
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses
Course of the Month7 days, 9 hours left to enroll