user6615498 user6615498 - 4 months ago 10
SQL Question

Max +1 with two digits always

I 'm using this code:

// example of my columns: 01
02
03


select max(panelname) + 1


I want my result have always two digits. Lets say that my value is >10 when adding +1 to take this result ex. from 01 to 02
when value is < 10 when adding +1 to take this result ex.from 23 to 24

Answer

You could do something like this:

SELECT CASE WHEN max(panelname) + 1 < 10 THEN '0' ELSE '' END + CONVERT(VARCHAR(2), max(panelname) + 1)