Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 28th, 2008, 04:21 PM
Authorized User
Join Date: Mar 2005
Location: , , .
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default DatePart Calculations

I have to create a report that runs every Monday, but pulls data from the previous Sunday to Saturday.

For example: The report ran on Feb 25th, but pulled data from Feb 17th(Sun) to Feb 23rd(Sat).

This is what I have so far, but can't figure out what I need:
FROM table1 
WHERE date >=  convert(datetime,convert(varchar(30),getdate() - DATEPART(dw,getdate()), 112) +2)
Can anyone help me with the correct query?


Reply With Quote
  #2 (permalink)  
Old February 28th, 2008, 07:09 PM
Friend of Wrox
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts

This code works for SQL Server 2000:

FROM table1
WHERE CONVERT(datetime, CONVERT(varchar(11), [date])) 
BETWEEN DATEADD(dd, -8, CONVERT(datetime, CONVERT(varchar(11), getdate())))
AND DATEADD(dd, -2, CONVERT(datetime, CONVERT(varchar(11), getdate()) + ' 23:59:59:998'))
If you run this on a day other than Monday, you will have to change the values of (-8) and (-2) as appropriate.

Reply With Quote
  #3 (permalink)  
Old February 29th, 2008, 03:08 AM
Authorized User
Join Date: Jun 2003
Location: Colombo 04, WP, Sri Lanka.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Preethi Send a message via Yahoo to Preethi

I assume that you have the default setting on the first day of the week. :)
This Piece of code Gets the Previous Saturday and the sunday before that day irrespective of the current day.
I have broken the code into multiple steps so that it will be easy to understand

SET @Today = DATEADD(mi, -1*DATEPART(mi, @Today), @Today) -- remove the minute component
SET @Today = DATEADD(hh, -1*DATEPART(hh, @Today), @Today) -- remove the hour component
SELECT * FROM table1
WHERE Date >=DATEADD(dd, -1*(DATEPART(dw, @Today)+6), @Today) AND
Date<DATEADD(dd, -1*(DATEPART(dw, @Today)+1), @Today)

-- It takes the date >= Sunday before last Saturday and Date< Last Friday. The Second part is important if you have date stored with time.


G.R.Preethiviraj Kulasingham
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Datepart Vision G Access 2 October 18th, 2006 06:56 AM
Make table Query and Datepart marshaji Access 1 June 21st, 2005 12:36 PM
CHapter 4 - using DatePart in query vgebha03 BOOK: Beginning Access VBA 0 June 2nd, 2005 02:48 PM
DatePart pab006 Classic ASP Basics 1 July 8th, 2004 12:50 PM
Datepart expression liz@trinityholdings.co.za Access 2 August 12th, 2003 07:31 AM

All times are GMT -4. The time now is 10:58 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.