从字符串中找出数字部分并将其存储为十进制数据类型
Finding out just the numeric part from string and storing it as decimal datatype
我尝试使用 patindex 查找字符串中数字值的开头。我的目标是只从不包括 % 的字符串中提取数字部分。
通过以下查询,这是我的结果:
SelectColumn_Desc,
替换(子字符串([Column_Desc], PatIndex('%[0-9]%', [Column_Desc]), len([Column_Desc])),'%',' ') 作为 New_Value
Column_Desc
- 我的税率是 18.8% **。
- 13.8% 是我的税率。
- 我的税率是 15.9% 你的呢?
New_Value
- 18.8
- 13.8 是我的税率。
- 15.9 你的是什么?
因此,结果(New_Value)应该是 18.8、13.8 和 15.9,数据类型为十进制。我无法让它工作。请指教。谢谢!
如果数字后面总是有一个百分号,您可以尝试以下方法,然后将结果转换或转换为小数。
Select
Column_Desc
,Replace(
substring([Column_Desc],
PatIndex('%[0-9]%', [Column_Desc]),
len([Column_Desc]))
,'%','') as New_Value
,PatIndex('%[0-9]%', [Column_Desc]) as pat_Value
,PatIndex('%[%]%', [Column_Desc]) as pct_Value
,SUBSTRING(
[Column_Desc],
PatIndex('%[0-9]%', [Column_Desc]),
PatIndex('%[%]%', [Column_Desc]) - PatIndex('%[0-9]%', [Column_Desc])
) as result_Value
From patindex
如果一个字符串中只能有一个这样的数字,并且它始终以数字开头(即没有像 '.75'
那样在小数点前省略 0 的值)并以数字结尾,则您可以通过将 patindex()
应用于字符串(就像您已经这样做的那样)来找到第一个数字,通过将 patindex()
应用于字符串的 reverse()
来找到最后一个数字。
SELECT convert(decimal(3, 1),
substring(column_desc,
patindex('%[0-9]%',
column_desc),
len(column_desc)
- patindex('%[0-9]%',
column_desc)
- patindex('%[0-9]%',
reverse(column_desc))
+ 2)) new_value
FROM elbat;
这将只为您提供 % 符号前的数字作为小数。您需要验证谓词中是否存在 %。
Select CAST(TRIM(SUBSTRING(Column_Desc,LOCATE('%',Column_Desc)-4,LOCATE('%',Column_Desc)-1)) 小数形式(3,1))
在您的问题中,您显示的结果为:
13.8 是我的税率。
如果那是一个字段,那么它必须是某种文本字段。那么你最好用 ''
替换 %
在你的例子中,数字都是 4 个字符长。如果这总是正确的,那么最简单的方法是:
select Column_Desc,
convert(decimal(5, 1),
left(stuff(Column_Desc, 1,
patindex('%[0-9]%', Column_Desc) - 1, ''), 4)
) as new_value
这里是db<>fiddle.
SQL服务器没有很好的字符串处理功能。因此,能够对您正在寻找的字符串做出这样的假设可以简化代码。
我尝试使用 patindex 查找字符串中数字值的开头。我的目标是只从不包括 % 的字符串中提取数字部分。
通过以下查询,这是我的结果:
SelectColumn_Desc, 替换(子字符串([Column_Desc], PatIndex('%[0-9]%', [Column_Desc]), len([Column_Desc])),'%',' ') 作为 New_Value
Column_Desc
- 我的税率是 18.8% **。
- 13.8% 是我的税率。
- 我的税率是 15.9% 你的呢?
New_Value
- 18.8
- 13.8 是我的税率。
- 15.9 你的是什么?
因此,结果(New_Value)应该是 18.8、13.8 和 15.9,数据类型为十进制。我无法让它工作。请指教。谢谢!
如果数字后面总是有一个百分号,您可以尝试以下方法,然后将结果转换或转换为小数。
Select
Column_Desc
,Replace(
substring([Column_Desc],
PatIndex('%[0-9]%', [Column_Desc]),
len([Column_Desc]))
,'%','') as New_Value
,PatIndex('%[0-9]%', [Column_Desc]) as pat_Value
,PatIndex('%[%]%', [Column_Desc]) as pct_Value
,SUBSTRING(
[Column_Desc],
PatIndex('%[0-9]%', [Column_Desc]),
PatIndex('%[%]%', [Column_Desc]) - PatIndex('%[0-9]%', [Column_Desc])
) as result_Value
From patindex
如果一个字符串中只能有一个这样的数字,并且它始终以数字开头(即没有像 '.75'
那样在小数点前省略 0 的值)并以数字结尾,则您可以通过将 patindex()
应用于字符串(就像您已经这样做的那样)来找到第一个数字,通过将 patindex()
应用于字符串的 reverse()
来找到最后一个数字。
SELECT convert(decimal(3, 1),
substring(column_desc,
patindex('%[0-9]%',
column_desc),
len(column_desc)
- patindex('%[0-9]%',
column_desc)
- patindex('%[0-9]%',
reverse(column_desc))
+ 2)) new_value
FROM elbat;
这将只为您提供 % 符号前的数字作为小数。您需要验证谓词中是否存在 %。
Select CAST(TRIM(SUBSTRING(Column_Desc,LOCATE('%',Column_Desc)-4,LOCATE('%',Column_Desc)-1)) 小数形式(3,1))
在您的问题中,您显示的结果为: 13.8 是我的税率。 如果那是一个字段,那么它必须是某种文本字段。那么你最好用 ''
替换 %在你的例子中,数字都是 4 个字符长。如果这总是正确的,那么最简单的方法是:
select Column_Desc,
convert(decimal(5, 1),
left(stuff(Column_Desc, 1,
patindex('%[0-9]%', Column_Desc) - 1, ''), 4)
) as new_value
这里是db<>fiddle.
SQL服务器没有很好的字符串处理功能。因此,能够对您正在寻找的字符串做出这样的假设可以简化代码。