0

我有这样的值'00100-01999' 我需要将这个值增加为'00100,00101,......01999'

我写了这样的脚本

DECLARE @vc_value VARCHAR(20),
    @min VARCHAR(10),
    @Max VARCHAR(10),
    @String VARCHAR(10),
    @Start BIGINT,
    @End BIGINT
CREATE TABLE #1 (ID VARCHAR(20))
SET @vc_value = '00100-01999'

SET @min = SUBSTRING(@vc_value, 1, CHARINDEX('-', @vc_value) - 1)

SET @Max = SUBSTRING(@vc_value, CHARINDEX('-', @vc_value) + 1, LEN(@vc_value))
SELECT @min,@max

WHILE CAST(@min AS INT) <= CAST(@Max AS INT)
BEGIN

    INSERT INTO #1
    SELECT CAST(@min AS VARCHAR(10))
    SET @min = @min + 1

    SELECT @min
END

SELECT * FROM #1

但我得到的结果是 100,101,....1999

我需要像'00100,00101,......01999'

4

1 回答 1

0

您需要添加所需数量的前导零。尝试这个:

WHILE CAST(@min AS INT) <= CAST(@Max AS INT)
BEGIN

    INSERT INTO #1
    SELECT RIGHT('00000'+ CAST(@min AS VARCHAR(10)),5)
    SET @min = @min + 1

    SELECT @min
END

这将附加'00000'到 100,101,....1999,给你'00000100'一个字符串,然后它会从该字符串中获取最后 5 个字符。

于 2013-09-24T06:48:46.877 回答