Pad a string with leading zeros so it's 3 characters long in SQL Server 2008

后端 未结 17 870
清歌不尽
清歌不尽 2020-11-22 08:13

I have a string that is up to 3 characters long when it\'s first created in SQL Server 2008 R2.

I would like to pad it with leading zeros, so if its original value w

相关标签:
17条回答
  • 2020-11-22 08:34

    Use this function which suits every situation.

    CREATE FUNCTION dbo.fnNumPadLeft (@input INT, @pad tinyint)
    RETURNS VARCHAR(250)
    AS BEGIN
        DECLARE @NumStr VARCHAR(250)
    
        SET @NumStr = LTRIM(@input)
    
        IF(@pad > LEN(@NumStr))
            SET @NumStr = REPLICATE('0', @Pad - LEN(@NumStr)) + @NumStr;
    
        RETURN @NumStr;
    END
    

    Sample output

    SELECT [dbo].[fnNumPadLeft] (2016,10) -- returns 0000002016
    SELECT [dbo].[fnNumPadLeft] (2016,5) -- returns 02016
    SELECT [dbo].[fnNumPadLeft] (2016,2) -- returns 2016
    SELECT [dbo].[fnNumPadLeft] (2016,0) -- returns 2016 
    
    0 讨论(0)
  • 2020-11-22 08:35

    Here is a variant of Hogan's answer which I use in SQL Server Express 2012:

    SELECT RIGHT(CONCAT('000', field), 3)
    

    Instead of worrying if the field is a string or not, I just CONCAT it, since it'll output a string anyway. Additionally if the field can be a NULL, using ISNULL might be required to avoid function getting NULL results.

    SELECT RIGHT(CONCAT('000', ISNULL(field,'')), 3)
    
    0 讨论(0)
  • 2020-11-22 08:35

    Wrote this because I had requirements for up to a specific length (9). Pads the left with the @pattern ONLY when the input needs padding. Should always return length defined in @pattern.

    declare @charInput as char(50) = 'input'
    
    --always handle NULL :)
    set @charInput = isnull(@charInput,'')
    
    declare @actualLength as int = len(@charInput)
    
    declare @pattern as char(50) = '123456789'
    declare @prefLength as int = len(@pattern)
    
    if @prefLength > @actualLength
        select Left(Left(@pattern, @prefLength-@actualLength) + @charInput, @prefLength)
    else
        select @charInput
    

    Returns 1234input

    0 讨论(0)
  • 2020-11-22 08:37

    I have always found the following method to be very helpful.

    REPLICATE('0', 5 - LEN(Job.Number)) + CAST(Job.Number AS varchar) as 'NumberFull'
    
    0 讨论(0)
  • 2020-11-22 08:39

    For those wanting to update their existing data here is the query:

    update SomeEventTable set eventTime=RIGHT('00000'+ISNULL(eventTime, ''),5)
    
    0 讨论(0)
  • 2020-11-22 08:41

    Although the question was for SQL Server 2008 R2, in case someone is reading this with version 2012 and above, since then it became much easier by the use of FORMAT.

    You can either pass a standard numeric format string or a custom numeric format string as the format argument (thank Vadim Ovchinnikov for this hint).

    For this question for example a code like

    DECLARE @myInt INT = 1;
    -- One way using a standard numeric format string
    PRINT FORMAT(@myInt,'D3');
    -- Other way using a custom numeric format string
    PRINT FORMAT(@myInt,'00#');
    

    outputs

    001
    001
    
    0 讨论(0)
提交回复
热议问题