December 3, 2005 at 11:30 am
How can I select records on a full date (datetime) when I need to select all records for the Current week, last week, current month and last month ?
Jean-Luc
www.corobori.com
December 3, 2005 at 12:34 pm
Use the Datediff function:
Current Week: DateDiff(wk,datefield,getdate()) = 0
Previous Week: DateDiff(wk,datefield,getdate()) = 1
Current Month: DateDiff(mm,datefield,getdate()) = 0
Previous Month: DateDiff(mm,datefield,getdate()) = 1
If the phone doesn't ring...It's me.
December 3, 2005 at 11:07 pm
Charles,
Very nice. And if you reverse the dates, the numbers make even more sense! I added a couple more to show the trend...
The only problem is that it will never do any better than an INDEX SCAN... do the following comparison with the "Execution Plan" turned on (run both at the same time)....
USE NORTHWIND
GO
SELECT SHIPPEDDATE
FROM Orders
WHERE DATEDIFF(wk,'07/22/1996',ShippedDate) = -1
SELECT ShippedDate
FROM Orders
WHERE ShippedDate>= DATEADD(wk,DATEDIFF(wk,0,'07/22/1996')-1,0)
AND ShippedDate< DATEADD(wk,DATEDIFF(wk,0,'07/22/1996'),0)
Basically, the second query should run 6.26 times faster when an index is present on the "datefield" or SHIPPEDDATE column... thats 626% times faster (if you like big numbers).
The good news is that if an index is not present on the column, they perform identically...
--Jeff Moden
Change is inevitable... Change for the better is not.
December 4, 2005 at 3:51 pm
Consider to take only last script from Jeff's post as an example.
All other ways will eliminate index on DateField and badly affect performance.
And "if an index is not present on the column" it must be created.
Moreover, if you use range select from the column this index MUST BE CLUSTERED.
_____________
Code for TallyGenerator
December 5, 2005 at 6:28 pm
Thanks,
Just one more thing: the SQL Server I am working with is US based, where the week starts on Sunday whereas I need to have the week starts on Monday. Anything I can do ?
Jean-Luc
www.corobori.com
December 5, 2005 at 7:37 pm
DateVal - ((DATEPART(dw, DateVal) + @@DATEFIRST + 5) %7)
gives you last monday before DateVal. If DateVal is Monday it will return DateVal.
_____________
Code for TallyGenerator
December 6, 2005 at 7:10 am
An easier way is to set the @DateFirst to show day 1 as Monday. Do this with:
set datefirst 1
If the phone doesn't ring...It's me.
July 24, 2012 at 4:09 am
Hi,
Can you help me in getting query to display current week number, previous 9 weeks & next 6 weeks for a year......
I have report requirent to display like current week number as 0 & previous 9 week no's as -9,-8,-7......like below
20124041424344454647484950
51 525312
Oct-12Nov-2012 Dec-2012Jan-2013
-9-8-7-6-5-4-3-2-10123456
Thanks
Gopi
July 24, 2012 at 7:04 am
84gopi (7/24/2012)
Hi,Can you help me in getting query to display current week number, previous 9 weeks & next 6 weeks for a year......
I have report requirent to display like current week number as 0 & previous 9 week no's as -9,-8,-7......like below
20124041424344454647484950
51 525312
Oct-12Nov-2012 Dec-2012Jan-2013
-9-8-7-6-5-4-3-2-10123456
Thanks
Gopi
There are 7 days in a week. Which one of them is the first day of the week for you?
--Jeff Moden
Change is inevitable... Change for the better is not.
July 24, 2012 at 7:15 am
84gopi (7/24/2012)
Hi,Can you help me in getting query to display current week number, previous 9 weeks & next 6 weeks for a year......
I have report requirent to display like current week number as 0 & previous 9 week no's as -9,-8,-7......like below
20124041424344454647484950
51 525312
Oct-12Nov-2012 Dec-2012Jan-2013
-9-8-7-6-5-4-3-2-10123456
Thanks
Gopi
Again dont hijack other peoples threads, you already have a thread for this question here
July 25, 2012 at 3:44 am
Hi,
If you consider zero as current week in then i should get previous 0 weeks like -9,-8,-7,-6 and the next 6 weeks from 0 like below .......
2012404142434445464748495051525312
Oct-12Nov-2012Dec-2012Jan-2013
-9-8-7-6-5-4-3-2-10123456
Please help me to with the function or expression ..........which displays above.........
Thanks
Gopi
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply