我需要将 int datafield 转换为带前导零的 nvarchar
例如:
1转换为’001’
转换为’000867’等。
thx.
这是我4小时后的反应。
我测试了这个 T-SQL 脚本,并为我工作的罚款!
DECLARE @number1 INT, @number2 INT
SET @number1 = 1
SET @number2 = 867
SELECT RIGHT('000' + CAST(@number1 AS NCHAR(3)), 3 ) AS NUMBER_CONVERTED
SELECT RIGHT('000000' + CAST(@number2 AS NCHAR(6)), 6 ) AS NUMBER_CONVERTED
I created this user function
T-SQL 代码:
CREATE FUNCTION CIntToChar(@intVal BIGINT, @intLen Int) RETURNS nvarchar(20)
AS
BEGIN
-- BIGINT = 2^63-1 (9,223,372,036,854,775,807) Max size number
-- @intlen contains the string size to return
IF @intlen > 20
SET @intlen = 20
RETURN REPLICATE('0',@intLen-LEN(RTRIM(CONVERT(nvarchar(20),@intVal))))
+ CONVERT(nvarchar(20),@intVal)
END
例如:
选择 dbo.CIntToChar (867,6)作为 COD _ ID
输出
000867