CIPHER CIPHER - 1 year ago 89
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

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

Returns a date of 2016-07-01

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download