我需要将int数据字段转换为前导零的nvarchar
例:
1转换为'001'
867转换为'000867'等
谢谢。
这是我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
我创建了这个用户功能
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)AS COD_ID
输出值
000867