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
 Fixed length text

Author  Topic 

rcp
Starting Member

32 Posts

Posted - 2012-01-09 : 08:01:42
Hi I am trying to collate year and week together. Works fine.

,CAST(YEAR(created_date) AS nVARCHAR(4)) + '/' + CAST(DATEPART(WW,created_Date) AS nVARCHAR(2)) "ComDate"

But what I would like is the week to be 2 characters long e.g. Week 1 in 2012 as 2012/01.

This is as far as I have got.

,(case when (LEN(DATEPART(WW,created_date)) = '2' then (DATEPART(WW,created_date)) else ('0'+DATEPART(WW,created_date)) end)

Any help would be much appreciated.

Roland

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-01-09 : 08:15:17
[code]
right( '00' + convert(varchar(2), < week no > ), 2)
[/code]


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

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-01-09 : 08:15:44

Replace getdate() with your column name

select datename(year,getdate())+right('00'+datename(week,getdate()),2)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-01-09 : 08:16:15


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-09 : 08:19:24
Just a note that the value returned by DATEPART(ww can change depending on the DATEFIRST setting. If there is the possibility that clients might change the DATEFIRST (manually or via some other queries/programs), you would need to take care of that. Alternatively, you may want to use ISO_WEEK.
Go to Top of Page

rcp
Starting Member

32 Posts

Posted - 2012-01-09 : 08:39:34
Once again, thank you all very much for you help. Roland
Go to Top of Page
   

- Advertisement -