[英]Remove Leading Zeros Except last Zero
數據庫-SQL Server 2012
我目前正在使用以下代碼:
substring(
SUBSTRING(col001, 59, 8),
patindex(
'%[^0]%',
SUBSTRING(col001, 59, 8)
),
10
) as TOTAL_DETAIL_RECORD_COUNT
我知道很多子字符串,但大多數情況下它都起作用。 不過有一個陷阱。 一些列值是000000000
。 在這種情況下,substring / patindex子句將保持原樣。 我可以做些什么來將值000000000
為返回0
。 只是一個零? 前導零的長度可能並不總是相同。
謝謝,格雷格
將子字符串轉換為INT,然后將其轉換回字符串
Select cast(cast('000000000' as int) as varchar(25)) -- Returns a string of 0
Select cast(cast('000000025' as int) as varchar(25)) -- Returns a string of 25
您沒有提及正在使用的數據庫。 在Oracle中,這將是:
select nvl(ltrim(substr(col001, 59, 8), '0'), '0')
from dual;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.