December 12, 2014 at 8:53 am
I am trying to query a table that has data stored week wise in columns like this ....
SELECT [Name]
,[Activity]
,[3-Nov-14]
,[10-Nov-14]
,[17-Nov-14]
,[24-Nov-14]
,[1-Dec-14]
,[8-Dec-14]
,[15-Dec-14]
,[22-Dec-14]
,[29-Dec-14]
,[YTD_2014]
From dbo.test
However , I want to query the data in the current week that is week of 8-dec-2014 without hard-coding the column (8-dec-2014)
Please suggest.
December 12, 2014 at 9:07 am
Try using this
declare @ThisDate datetime;
set @ThisDate = getdate();
select dateadd(wk, datediff(wk, 0, @ThisDate), 0)
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
December 12, 2014 at 9:08 am
Thank you!
December 12, 2014 at 9:10 am
You're welcome.
http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/ has a lot of useful date things if you need more.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
December 12, 2014 at 12:45 pm
BWFC (12/12/2014)
Try using this
declare @ThisDate datetime;
set @ThisDate = getdate();
select dateadd(wk, datediff(wk, 0, @ThisDate), 0)
Be advised that may not work as expected on all installations because the WK datepart boundaries are based on the value of DATEFIRST, which can vary even with something as simple as a language change (for example).
If you want to force dates that "start on Mondays", you might want to use the following...
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())/7*7,0)
It looks weird to first divide by 7 and then turn right around and multiply by 7 but it's integer math. The divide will drop the partial week (partial 7 day period).
The "0" date is the First of January, 1900, which was a Monday.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 12, 2014 at 2:18 pm
Great! Thank you
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply