Solved

Get Tuesday through Monday in query

Posted on 2013-10-22
3
285 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
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
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
Comment Utility
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 65

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now