SQL Server The number 0 in front is missing as a result of the addition [closed]

92dk7w1h  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(77)

Closed. This question is not reproducible or was caused by typos . It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 6 days ago.
Improve this question

I want to add up a value in SQL Server but the first 0 is missing after adding it The following is an example of the code:

DECLARE @test1 VARCHAR(8)
DECLARE @test2 INT

SET @test2 = 1
SET @test1 = '06149' + @test2
SELECT @test1

Output: 6150

what I want is a result like this: 06150

j91ykkif

j91ykkif1#

Other Way(SQL Server):

DECLARE @test1 VARCHAR(8)
DECLARE @test2 INT

SET @test2 = 1
SET @test1 = '06149' + @test2
SELECT FORMAT(CAST(@test1 AS INT), '00000')
lf5gs5x2

lf5gs5x22#

I've found the answer:

DECLARE @empNumber INT = 6149
SELECT STUFF('00000', 5-LEN(@empNumber)+1, LEN(@empNumber), @empNumber)

相关问题