Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 datepart(wk, date) not working properly

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2014-01-11 : 05:19:39
Hi,
When I use the following query, it shows only dates from 1st Jan 2014 to 4th Jan 2014, but week 1 should have been 30th Dec to Jan 5th. Can you tell me where I'm doing wrong?


select date from table
where datepart(wk,date) = 1

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-01-11 : 08:24:02
what you want is iso_week

datepart(iso_week,date)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

learning_grsql
Posting Yak Master

230 Posts

Posted - 2014-01-11 : 09:45:30
I'm using 2005 sql server. iso_week is not a recognized option
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-11 : 12:39:29
so whats your exact requirement?to get all days in 1st week of year?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

learning_grsql
Posting Yak Master

230 Posts

Posted - 2014-01-12 : 06:13:11
Hi Visakh16, I want to extract a data for 1st week of this year. My query looks like below.

select date, sum(price) from table
where datepart(wk,date) = 1
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-01-12 : 06:27:02
quote:
Originally posted by learning_grsql

I'm using 2005 sql server. iso_week is not a recognized option


use this
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=60510


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -