CIPHER CIPHER - 2 months ago 5x
SQL Question

Subtract month from yearmonth

In SQL I have a criteria to subtract one month from the YYYYMM value. Kindly help me. I able to get answer with below

select 201608 - 1;

But when it is 01 month, It doesn't works.

Help me on this, without checking 01 month in if condition !

I also went through DATADD and DATEDIFF functions, but couldn't able to get results as expected

Select DateAdd(MM,-1,cast(cast(201608 as varchar(10))+'01' as date))

Returns a date of 2016-07-01