May 12, 2010 at 1:36 am
Hello expert,
Using this sql statement datepart (week, '2005-01-01') returns 1 for week 1, I would like to return in this way:
00
01
02
.
.
.
51
Could anybody help me to get the result in 2 digits?
Thanks
May 12, 2010 at 2:57 am
Hi,
SELECT RIGHT('0' + CAST(datepart (week, '2005-01-01') AS VARCHAR(2)),2)
should work. I'm just putting a zero in front and then take 2 places from the right.
Steffen.
May 12, 2010 at 3:16 am
Select Replicate('0',2-Len(datepart (week, '2005-01-09'))) + Cast(datepart (week, '2005-01-09') as varchar(10)) as DOW
May 12, 2010 at 3:20 am
shadow_2 (5/12/2010)
Hi,
SELECT RIGHT('0' + CAST(datepart (week, '2005-01-01') AS VARCHAR(2)),2)
should work. I'm just putting a zero in front and then take 2 places from the right.
Steffen.
Sorry, I overlooked that you want to start with week 0, so just substract 1 from the datepart result.
SELECT RIGHT('0' + CAST((datepart (week, '2005-01-01') - 1) AS VARCHAR(2)),2)
Hope that helps,
Steffen.
May 12, 2010 at 8:00 am
If you want it that way, then you will have to loose the INT data-type and your string will be of VARCHAR..
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply