1

我需要在我的表中插入一个数字0000128,但它将此数字视为128,我也尝试将类型从 更改intvarchar,但没有区别。

有没有可能做到这一点?

4

3 回答 3

3

存储varchar应该可以正常工作。

DECLARE @T TABLE (
  V VARCHAR(10));

INSERT INTO @T
VALUES      ('0000128');

SELECT *
FROM   @T 

您必须在此过程中转换为数字并丢失前导零。

于 2013-11-02T20:46:00.343 回答
0

没有前导零的数字,只有前导零的数字的文本表示

于 2013-11-02T20:21:02.307 回答
0

我之前在做 ETL 时遇到过这个问题。这就是我解决它的方法。

-- declare and assign
-- this could be from anywhere
declare @int_NUMBER int = 128

-- show the original
select @int_NUMBER

-- steps:
-- first: cast the number as a varchar

-- second: create a string of zeros. The number of zeros should be equal to the max size of this "number". 
-- In this example, the max size this "number" is 7 places.

-- third: concat the leading zeros in front of the varchar of the number

-- forth: take the number of max places from the right
-- this way you will always get the proper amount of leading zeros

select right('0000000' + cast(@int_NUMBER as varchar(10)),7)

结果:


128

(1 行受影响)


0000128

(1 行受影响)

于 2013-11-02T21:29:04.800 回答