Rogger Fernandes Rogger Fernandes - 4 months ago 16
SQL Question

Increment month number by 1 SQL

I need to update a column value incrementing by 1, the column is month_number and it is defined as integer, when the month is 12, I need to update to 1 instead of 13

I first tried this approach:

update tab
set col_month = col_month + 1


But won't work for cases where col_month is 12

Answer

Here's one method using a case statement:

update tab
set col_month = case when col_month = 12 then 1 else col_month + 1 end