jon.nicholssoftware.com jon.nicholssoftware.com - 1 month ago 17
SQL Question

Concat 2 varchars in SQL

I am trying to produce a varchar(20) which is 4 character year + 2 character month. I would like to then use this as a parameter in my stored procedure. Im unable to concatenate 2 varchars together to produce this, any ideas?

SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) AS CurrentYear
, Convert(VARCHAR(20),MONTH(GETDATE())) AS CurrentMonth
, CurrentYear + CurrentMonth AS YearMonth

Answer

Like this one?

SELECT GETDATE() AS CurrentDateTime
 , Convert(VARCHAR(20),YEAR(GETDATE())) AS CurrentYear
 , Convert(VARCHAR(20),MONTH(GETDATE())) AS CurrentMonth
 , Convert(VARCHAR(20),YEAR(GETDATE())) + Convert(VARCHAR(20),MONTH(GETDATE())) AS YearMonth
Comments