SQLTeam.com | Weblogs | Forums

Concatenate YYYY and MM


#1

Hi
How do I concatenate YYYY and MM to get YYYYMM without getting the '2017011' problem and without using a CASE statement?

Currently using code below but need to exclude the zero for months 10, 11, 12.

CAST(AccountYear as varchar(4)) + '0' + CAST(AccountMonth as varchar(2))

thanks


#2
CAST(AccountYear as varchar(4)) + RIGHT('0' + CAST(AccountMonth as varchar(2)), 2)

#3

or (if integers)

AccountYear*100 + AccountMonth


#4

To finish gbritton's approach:

CAST(AccountYear*100 + AccountMonth AS varchar(6))


#5

Thankyou all. Ideally I need the end result as an INT. How do I build that in without creating a separate object?


#6

like that