I have a varchar field that contains numbers in this format "00001" , "00002" etc. When I try to get the next number by using Max(Field) + 1, I get an integer "3" for example.
How can I get the result "00003" instead of "3"?
Here's an working example I've just figure it out, but i think there's must be an easier way:
SELECT TOP (1)
{ fn REPEAT(0, LEN(ItemId) - LEN(MAX(ItemId) + 1)) } +
CAST(MAX(ItemId) + 1 AS varchar(7)) AS Expr1
FROM Items
GROUP BY ItemId
ORDER BY ItemId DESC
The last query gives the correct result "0004916"