Solved

Get Tuesday through Monday in query

Posted on 2013-10-22
3
295 Views
Last Modified: 2013-10-30
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
Comment
Question by:williamss132
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 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()) 

Open in new window

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 

Open in new window

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

by:COANetwork
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);

Open in new window

Assign this to a date variable, then do a
SELECT @myMondayDate = DATEADD(day, 6, @myTuesdayDate)

Open in new window

and you get your next Monday.  Then use these values as a range for your select
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39611684
Thanks for the grade.  Good luck with your code.  -Jim
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question