Skip to content
Advertisement

Convert/get varchar variable to YYYYMM

I have 4 CTE’s in this table and the third one contains a DATETIME converted to VARCHAR (with format based on the requirement) as startDate in DD/MM/YYYY format. The last cte does calculations based on the data generated and one of the columns needs to store YYYYMM date based on startDate.

The problem it’s getting the year and the month from this converted DATETIME, using convert() it shows this:

These 2 show YYYYMM correctly when startDate isn’t converted:

How could I get YYYYMM format having startDate converted? Or what could be a more smart approach to the requirement

Advertisement

Answer

My way would be slightly different

Here, I first converted it to date and then formatted to YYYYMMDD and taken 6 chars only

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement