Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Month to 2 digits

Status
Not open for further replies.

corbinap

Programmer
Nov 20, 2006
34
0
0
US
How do I convert the month to return a 2 digit month - not 1????

Declare @LastMonth varchar(2)
SET @LastMonth = convert(varchar(2), Datepart(mm,getdate())-1);
select @LastMonth
 
Here's one way:
Code:
Declare @LastMonth varchar(2)
SET @LastMonth = [!]right('00' + [/!]convert(varchar(2), Datepart(mm,getdate())-1)[!], 2)[/!];
select @LastMonth

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
But, I also need to add the 4 digits year in the front: yyyymm

Declare @LastMonth varchar(2)
SET @LastMonth = right('00' + convert(varchar(2), Datepart(mm,getdate())-1), 2);
select @LastMonth

 
select lastmonth=convert(varchar(6),dateadd(mm,datediff(mm,0,getdate())-1,0),112)
 
Actually, since we ignore the day part:
select lastmonth=convert(varchar(6),dateadd(mm,-1,getdate()),112)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top