How to increment this [Resolved]

Posted by Jayakumars under .NET Framework on 3/3/2016 | Points: 10 | Views : 457 | Status : [Member] [MVP] | Replies : 2
Hi

How to increment and show this

I tried in sql server this

SELECT '000009'+1

But here now output shows

10

only

But i need the output this

000010


how will do this

Mark as Answer if its helpful to you


Responses

Posted by: A2H on: 3/3/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
If you are getting 10 that means the value is converted to integer. You may consider converting the value back to varchar and then use Replicate function to add Zeros
Sample

DECLARE @INPUTSTRING INT 
SET @INPUTSTRING = '000009'+1
SELECT REPLICATE('0', 6 - LEN(@INPUTSTRING)) + CAST (@INPUTSTRING AS VARCHAR) AS FORMATTEDVALUE


Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Professionaluser on: 3/4/2016 [Member] [MVP] Bronze | Points: 25

Up
0
Down
DECLARE @Num varchaR(10) = '000009'
select right('000000'+ cast(@Num+1 as varchar), 6) as NextNum


Output:

NextNum
000010


Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response