1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | DECLARE @Number INT = 123 DECLARE @Digits INT = 8 DECLARE @Result CHAR(8) -- 第一種 SELECT @Result = RIGHT( CAST( POWER(10, @Digits) + @Number AS VARCHAR(9)) , @Digits) SELECT @Result -- 第二種 SELECT @Result = REPLICATE('0', @Digits - DATALENGTH(CAST(@Number AS VARCHAR(8)))) + CAST(@Number as VARCHAR(8)) SELECT @Result |
Direct link: https://paste.plurk.com/show/256879