用前導零填充字符串,因此在SQL Server 2008中爲3個字符長

本文翻譯自:Pad a string with leading zeros so it's 3 characters long in SQL Server 2008

I have a string that is up to 3 characters long when it's first created in SQL Server 2008 R2. 在SQL Server 2008 R2中首次創建時,該字符串的長度最多爲3個字符。

I would like to pad it with leading zeros, so if its original value was '1' then the new value would be '001'. 我想用前導零填充它,因此,如果其原始值是“ 1”,那麼新值將是“ 001”。 Or if its original value was '23' the new value is '023'. 或者,如果其原始值爲'23',則新值爲'023'。 Or if its original value is '124' then new value is the same as original value. 或者,如果其原始值爲“ 124”,則新值與原始值相同。

I am using SQL Server 2008 R2. 我正在使用SQL Server 2008 R2。 How would I do this using T-SQL? 我將如何使用T-SQL做到這一點?


#1樓

參考:https://stackoom.com/question/18KH6/用前導零填充字符串-因此在SQL-Server-中爲-個字符長


#2樓

If the field is already a string, this will work 如果該字段已經是字符串,則可以使用

 SELECT RIGHT('000'+ISNULL(field,''),3)

If you want nulls to show as '000' 如果您希望null顯示爲'000'

It might be an integer -- then you would want 它可能是整數-那麼您需要

 SELECT RIGHT('000'+CAST(field AS VARCHAR(3)),3)

As required by the question this answer only works if the length <= 3, if you want something larger you need to change the string constant and the two integer constants to the width needed. 根據問題的要求,此答案僅在長度<= 3時有效,如果要更大,則需要將字符串常量和兩個整數常量更改爲所需的寬度。 eg '0000' and VARCHAR(4)),4 例如'0000' and VARCHAR(4)),4


#3樓

對於整數,可以使用從int到varchar的隱式轉換:

SELECT RIGHT(1000 + field, 3)

#4樓

對於那些想要更新其現有數據的人,這裏是查詢:

update SomeEventTable set eventTime=RIGHT('00000'+ISNULL(eventTime, ''),5)

#5樓

Here's a more general technique for left-padding to any desired width: 這是一種用於將左側填充到任何所需寬度的更通用的技術:

declare @x     int     = 123 -- value to be padded
declare @width int     = 25  -- desired width
declare @pad   char(1) = '0' -- pad character

select right_justified = replicate(
                           @pad ,
                           @width-len(convert(varchar(100),@x))
                           )
                       + convert(varchar(100),@x)

However, if you're dealing with negative values, and padding with leading zeroes, neither this, nor other suggested technique will work. 但是,如果要處理負值,並用前導零填充,則此方法或其他建議的方法都將無效。 You'll get something that looks like this: 您將獲得如下所示的內容:

00-123

[Probably not what you wanted] [可能不是您想要的]

So … you'll have to jump through some additional hoops Here's one approach that will properly format negative numbers: 所以……您將不得不跳過一些額外的步驟,這是一種可以正確設置負數格式的方法:

declare @x     float   = -1.234
declare @width int     = 20
declare @pad   char(1) = '0'

select right_justified = stuff(
         convert(varchar(99),@x) ,                            -- source string (converted from numeric value)
         case when @x < 0 then 2 else 1 end ,                 -- insert position
         0 ,                                                  -- count of characters to remove from source string
         replicate(@pad,@width-len(convert(varchar(99),@x)) ) -- text to be inserted
         )

One should note that the convert() calls should specify an [n]varchar of sufficient length to hold the converted result with truncation. 應該注意的是, convert()調用應指定一個[n]varchar ,其長度應足以截斷轉換後的結果。


#6樓

我一直髮現以下方法非常有幫助。

REPLICATE('0', 5 - LEN(Job.Number)) + CAST(Job.Number AS varchar) as 'NumberFull'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章