Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 28th, 2008, 04:21 PM
Authorized User
Join Date: Mar 2005
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?


Old February 28th, 2008, 07:09 PM
Friend of Wrox
Join Date: Jun 2003
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.

Old February 29th, 2008, 03:08 AM
Authorized User
Join Date: Jun 2003
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

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

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