Reputation:
I have a column named [Month] which has the months as such..
1
2
3
4
5
6
7
8
9
10
11
12
What I want to be able to do is for the first 1-9 is add a 0 so that they read...
01
02
03
04
05
06
07
08
09
10
11
12
How would I do this via a derived column? (running SQL Server 2012)
Thanks for your help.
Upvotes: 0
Views: 425
Reputation:
after add derived column in expression write this code
RIGHT("00"+(DT_WSTR,2)Month,2)
Upvotes: 2
Reputation: 9724
Data:
1, 2, 3, 4, 10, 15, 100
Derived column code:
Month < 10 ? "0" + (DT_WSTR,3)Month : (DT_WSTR,3)Month
Result:
Upvotes: 1
Reputation: 2006
select
case
when [Month] < 10 then '0'
else ''
end + ltrim(rtrim(cast([Month] as varchar)))
Upvotes: 0