Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Zero Padding using getdate

Author  Topic 

GeoffreyP
Starting Member

9 Posts

Posted - 2009-11-05 : 08:47:48
How do I get the month to be 09
Code Below

Declare
@Period varchar(6),
@gYear Varchar(4),
@gMonth varChar(2),
@LongPeriod varChar(30),
@LongMonth varchar(20)

Select
@gYear = Year(getdate()),
@gMonth = (Month(getdate()) -2),
@Period = @gYear + @gMonth,
@LongMonth = DATENAME(MONTH,getdate()),
@LongPeriod = @LongMonth + ' ' + @gYear



Select
@gYear as [Year],
@gMonth as [Month],
@Period as Period,
@LongMonth as longMth,
@LongPeriod as longPeriod

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-05 : 08:53:27
select right('00'+convert(varchar(2),@gMonth),2) as [Month]


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

GeoffreyP
Starting Member

9 Posts

Posted - 2009-11-05 : 09:03:23
Thanks It works
Go to Top of Page

GeoffreyP
Starting Member

9 Posts

Posted - 2009-11-05 : 09:23:42
quote:
Originally posted by webfred

select right('00'+convert(varchar(2),@gMonth),2) as [Month]


No, you're never too old to Yak'n'Roll if you're too young to die.


If it's to loud you'r to old
Go to Top of Page

GeoffreyP
Starting Member

9 Posts

Posted - 2009-11-05 : 09:33:07
Check the line below: does supply
correct long month???
Year Month Period LongMth LongPeriod
2009 09 200909 January January 2009

Select @LongMonth = DATENAME(MONTH,(Month(getdate())-2 )),
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-05 : 09:50:54
Not sure what you mean.

set language english
select DATENAME(MONTH,(Month(getdate())-2 ))

gives:
January

BUT:
select datename(month,(dateadd(month,-2,getdate())))
gives:
September


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -