How to return week number in 2 digit form

  • 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

  • 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.

  • Select Replicate('0',2-Len(datepart (week, '2005-01-09'))) + Cast(datepart (week, '2005-01-09') as varchar(10)) as DOW

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

  • 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.

  • 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