Skip to content
Advertisement

SQL concat case select when trying to add new column of previous month data

I have a data set in which I need to concat month and year for the previous month. the problem is it spans over two years. I need to create a statement in which mean the month – 1 = 0, month becomes 12 and year-1.

Select concat(case
when month > 1 then select(month-1, year)
when month = 1 then select(12, year -1)
else select(month-1, year)
end

as monthyear from table

Advertisement

Answer

You need 2 CASE statements inside concat(), for the month and the year:

select 
  concat(
    case when month > 1 then month - 1 else 12 end,
    case when month > 1 then year else year - 1 end
  )
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement