Increment a varchar in SQL

后端 未结 2 986
情歌与酒
情歌与酒 2021-01-26 13:07

Basically, I want to increment a varchar in SQL the has a value of \"ABC001\".

I have code that adds one to an int, but I don\'t know how to get it working for a varchar

2条回答
  •  失恋的感觉
    2021-01-26 13:08

    update 
        [table] 
    set [nxt_no] = case when PATINDEX('%[0-9]%', [nxt_no]) > 0 then 
              left([nxt_no], PATINDEX('%[0-9]%', [nxt_no])-1) -- Text part
              + -- concat
              right( REPLICATE('0', LEN([nxt_no]) - PATINDEX('%[0-9]%', [nxt_no])+1) + convert( varchar, convert(int, right([nxt_no], LEN([nxt_no]) - PATINDEX('%[0-9]%', [nxt_no])+1))+1), LEN([nxt_no]) - PATINDEX('%[0-9]%', [nxt_no])+1)
    else 
        [nxt_no] end
    

提交回复
热议问题