user3583912
user3583912

Reputation: 1322

T-SQL concatenate an incremental row number

I want to increase the number by concatenating with some value.

for Ex:

declare @i int
select @i =1
select right('000' + convert(varchar(3),@i),3)

output is : 001

if my @i value increases to 1005

then the output should be: 1005

I know I can increase the number '0000' , but I want this start with 3 digits then if @i value reach 1000 then I want in 4 digits like this 1000,1001,1002...

is there any way to get this.. Thanks in advance.

Upvotes: 0

Views: 191

Answers (1)

Joe Stefanelli
Joe Stefanelli

Reputation: 135808

SELECT CASE WHEN @i < 1000 THEN RIGHT('000' + CONVERT(VARCHAR(3),@i),3)
            ELSE CONVERT(VARCHAR(4),@i)
       END /* CASE */

Upvotes: 2

Related Questions