Thursday, March 8, 2012

Datetime Conversion Question

If I have a variables that are declared as

@.Start_Year int

@.Start_Month int

How do I convert those variables to datetime format of

MM/1/YYYY

If what you want is datetime:

select convert(datetime, convert(varchar(2), @.start_month)

+ '/1/' + convert(char(4), @.start_year))

If what you want is a formated string in which the month contains a potential leading zero:

select right('0' + convert(varchar(2), @.start_month), 2) +

'/1/' + convert(char(4), @.start_year)

No comments:

Post a Comment