March 7, 2007 at 4:21 am
How do I receive the month in 2 digits in select (mm,getdate())? Every month earlier then October is returned as 1 digit. Funny thing, when I execute
select
right(datepart(mm,getdate()),2)
I receive '07'...
I can construct some SQL to accomplish this, but I would think it would be easy to format the output.
Greetz,
Hans Brouwer
March 7, 2007 at 5:25 am
Not that I know of so I would just use this:
select
'Month' =
case len(datepart(mm,getdate()))
when 1 then
'0' + cast(datepart(mm,getdate()) as varchar)
else
cast(datepart(mm,getdate()) as varchar)
end
March 7, 2007 at 6:01 am
Tnx for answering, Aiden. I had something similar, but was hoping for a more elegant solution. I can't find it, so I guess I'll have to use the brute-force approach....
Greetz,
Hans Brouwer
March 7, 2007 at 6:08 am
SELECT REPLACE(STR(MONTH(GETDATE()), 2), ' ', '0')
March 7, 2007 at 5:24 pm
select MM = right(100+month(getdate()),2)
MM ---- 03
(1 row(s) affected)
March 8, 2007 at 1:40 pm
Tnx for the answers, really helped me.
Greetz,
Hans Brouwer
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply