Search code examples
sqlsql-servert-sqlweek-numberdatepart

TSQL DatePart Week in ww format 01 instead of 1


What is the best way to get the week from a date in the format 01 02 03 04 05 rather than 1 2 3 4 5?


Solution

  • The simplest method is to concatenate your leading 0s and then taking the two rightmost values:

    select right(concat('00', DATEPART(week, getdate())), 2) as WeekNum
    

    You do not need a udf for this...