Search code examples
sqlsql-server-2000

Select cast with max +1 from textstring


I'm using this code but i get Conversion failed when converting the varchar value 'o' to data type int.

my columns contain example

o01
o02
o13

select cast('o'+(max(right(panelname, 2 )+1))as varchar(50))

Should use Convert thats why i get this errror?

It was syntax error, Solved Like this,but now in values >10 i want to take tis result o06, now i take o6

select 'o' + cast(max(right(panelname, 2) + 1) as varchar(50))

Solution

  • You are adding one to a string. 1 is a number so + is interpreted as addition rather than concatenation.

    Perhaps you intend:

    select cast('o' + (max(right(panelname, 2 ) + '1')) as varchar(50))
    

    Alternatively, you might want:

    select 'o' + cast(max(right(panelname, 2) + 1) as varchar(50))
    

    This assume that the last two characters of panelname are digits.