相比之下,说:

REPLICATE(@padchar, @len - LEN(@str)) + @str

当前回答

我知道这个问题最初是在2008年提出的,但是在SQL Server 2012中引入了一些新函数。FORMAT函数很好地简化了以零结尾的填充。它也会为你执行转换:

declare @n as int = 2
select FORMAT(@n, 'd10') as padWithZeros

更新:

我想亲自测试FORMAT函数的实际效率。我很惊讶地发现,与AlexCuse的原始答案相比,效率不是很好。虽然我发现FORMAT函数更简洁,但就执行时间而言,它不是很高效。我使用的tallytable有64,000条记录。Martin Smith指出了执行时间效率。

SET STATISTICS TIME ON
select FORMAT(N, 'd10') as padWithZeros from Tally
SET STATISTICS TIME OFF

SQL Server执行次数: CPU时间= 2157 ms,运行时间= 2696 ms。

SET STATISTICS TIME ON
select right('0000000000'+ rtrim(cast(N as varchar(5))), 10) from Tally
SET STATISTICS TIME OFF

SQL Server执行次数:

CPU时间= 31 ms,运行时间= 235 ms。

其他回答

也许是一个过度杀戮,我有这些udf垫左和右

ALTER   Function [dbo].[fsPadLeft](@var varchar(200),@padChar char(1)='0',@len int)
returns varchar(300)
as
Begin

return replicate(@PadChar,@len-Len(@var))+@var

end

向右

ALTER function [dbo].[fsPadRight](@var varchar(200),@padchar char(1)='0', @len int) returns varchar(201) as
Begin

--select @padChar=' ',@len=200,@var='hello'


return  @var+replicate(@PadChar,@len-Len(@var))
end

这是我通常如何填充一个varchar

WHILE Len(@String) < 8
BEGIN
    SELECT @String = '0' + @String
END

我有一个函数lpad有x个小数 创建函数[dbo].[LPAD_DEC] ( ——在这里添加函数的参数 @pad nvarchar (MAX), @string nvarchar (MAX), @length int, @dec int ) 返回nvarchar (max) 作为 开始 ——在这里声明返回变量 声明@resp nvarchar(max)

IF LEN(@string)=@length
BEGIN
    IF CHARINDEX('.',@string)>0
    BEGIN
        SELECT @resp = CASE SIGN(@string)
            WHEN -1 THEN
                -- Nros negativos grandes con decimales
                concat('-',SUBSTRING(replicate(@pad,@length),1,@length-len(@string)),ltrim(str(abs(@string),@length,@dec)))
            ELSE
                -- Nros positivos grandes con decimales
                concat(SUBSTRING(replicate(@pad,@length),1,@length-len(@string)),ltrim(str(@string,@length,@dec)))                  
            END
    END
    ELSE
    BEGIN
        SELECT @resp = CASE SIGN(@string)
            WHEN -1 THEN
                --Nros negativo grande sin decimales
                concat('-',SUBSTRING(replicate(@pad,@length),1,(@length-3)-len(@string)),ltrim(str(abs(@string),@length,@dec)))
            ELSE
                -- Nros positivos grandes con decimales
                concat(SUBSTRING(replicate(@pad,@length),1,@length-len(@string)),ltrim(str(@string,@length,@dec)))                  
            END                     
    END
END
ELSE
    IF CHARINDEX('.',@string)>0
    BEGIN
        SELECT @resp =CASE SIGN(@string)
            WHEN -1 THEN
                -- Nros negativos con decimales
                concat('-',SUBSTRING(replicate(@pad,@length),1,@length-len(@string)),ltrim(str(abs(@string),@length,@dec)))
            ELSE
                --Ntos positivos con decimales
                concat(SUBSTRING(replicate(@pad,@length),1,@length-len(@string)),ltrim(str(abs(@string),@length,@dec))) 
            END
    END
    ELSE
    BEGIN
        SELECT @resp = CASE SIGN(@string)
            WHEN -1 THEN
                -- Nros Negativos sin decimales
                concat('-',SUBSTRING(replicate(@pad,@length-3),1,(@length-3)-len(@string)),ltrim(str(abs(@string),@length,@dec)))
            ELSE
                -- Nros Positivos sin decimales
                concat(SUBSTRING(replicate(@pad,@length),1,(@length-3)-len(@string)),ltrim(str(abs(@string),@length,@dec)))
            END
    END
RETURN @resp

END

无论如何,这都是一种低效的SQL使用。

也许是这样的

right('XXXXXXXXXXXX'+ rtrim(@str), @n)

其中X是填充字符,@n是结果字符串中的字符数(假设您需要填充,因为您处理的是固定长度)。

但我说过,应该避免在数据库中这样做。

我不确定你给出的方法真的是低效的,但另一种方法,只要它不需要灵活的长度或填充字符,将是(假设你想用“0”到10个字符填充它:

DECLARE
   @pad_characters VARCHAR(10)

SET @pad_characters = '0000000000'

SELECT RIGHT(@pad_characters + @str, 10)