SQLTeam.com | Weblogs | Forums

Concatenate YYYY and MM

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))


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

or (if integers)

AccountYear*100 + AccountMonth

1 Like

To finish gbritton's approach:

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

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

like that