我有一张桌子,它有3列。 first column
是包含值(数字)和单位(百分比等等)的数据,第二列是 numeric column
,第三是 Unit column
。我想要做的是从第一列拆分数字和单位,然后将那些拆分数据放到指定的列。
这是我的表:
我尝试了这个函数:SO link here...,它确实拆分了alpha和numeric,但后来我使用了SQL函数,我的问题是参数必须在字符串STRING
中,所以我做的是将其更改为Sub Query但它给了我错误。
示例COde:
SQL功能:
create function [dbo].[GetNumbersFromText](@String varchar(2000))
returns table as return
(
with C as
(
select cast(substring(S.Value, S1.Pos, S2.L) as int) as Number,
stuff(s.Value, 1, S1.Pos + S2.L, '') as Value
from (select @String+' ') as S(Value)
cross apply (select patindex('%[0-9]%', S.Value)) as S1(Pos)
cross apply (select patindex('%[^0-9]%', stuff(S.Value, 1, S1.Pos, ''))) as S2(L)
union all
select cast(substring(S.Value, S1.Pos, S2.L) as int),
stuff(S.Value, 1, S1.Pos + S2.L, '')
from C as S
cross apply (select patindex('%[0-9]%', S.Value)) as S1(Pos)
cross apply (select patindex('%[^0-9]%', stuff(S.Value, 1, S1.Pos, ''))) as S2(L)
where patindex('%[0-9]%', S.Value) > 0
)
select Number
from C
)
使用SUB查询选择语句:
declare @S varchar(max)
select number from GetNumbersFromText(Select SomeColm From Table_Name) option (maxrecursion 0)
BTW,我正在使用sql server 2005。
谢谢!
答案 0 :(得分:10)
如果数字部分始终位于开头,则可以使用:
PATINDEX('%[0-9][^0-9]%', ConcUnit)
获取最后一位数的索引。
因此,这个:
DECLARE @str VARCHAR(MAX) = '4000 ug/ML'
SELECT LEFT(@str, PATINDEX('%[0-9][^0-9]%', @str )) AS Number,
LTRIM(RIGHT(@str, LEN(@str) - PATINDEX('%[0-9][^0-9]%', @str ))) As Unit
给你:
Number Unit
-------------
4000 ug/ML
修改强>
如果数字数据也包含双精度值,那么您可以使用:
SELECT LEN(@str) - PATINDEX ('%[^0-9][0-9]%', REVERSE(@str))
获取最后一位数的索引。
因此,这个:
SELECT LEFT(@str, LEN(@str) - PATINDEX ('%[^0-9][0-9]%', REVERSE(@str)))
为您提供数字部分。
而且:
SELECT LEFT(@str, LEN(@str) - PATINDEX ('%[^0-9][0-9]%', REVERSE(@str))) AS Numeric,
CASE
WHEN CHARINDEX ('%', @str) <> 0 THEN LTRIM(RIGHT(@str, LEN(@str) - CHARINDEX ('%', @str)))
ELSE LTRIM(RIGHT(@str, PATINDEX ('%[^0-9][0-9]%', REVERSE(@str))))
END AS Unit
为您提供数字和单位部分。
以下是我对您发布的数据进行的一些测试:
输入:
DECLARE @str VARCHAR(MAX) = '50 000ug/ML'
输出:
Numeric Unit
------------
50 000 ug/ML
输入:
DECLARE @str VARCHAR(MAX) = '99.5%'
输出:
Numeric Unit
------------
99.5
输入:
DECLARE @str VARCHAR(MAX) = '4000 . 35 % ug/ML'
输出:
Numeric Unit
------------------
4000 . 35 ug/ML
答案 1 :(得分:3)
这是我的答案。检查SQLFiddle中的输出是否相同。
create TABLE temp
(
string NVARCHAR(50)
)
INSERT INTO temp (string)
VALUES
('4000 ug\ml'),
('2000 ug\ml'),
('%'),
('ug\ml')
SELECT subsrtunit,LEFT(subsrtnumeric, PATINDEX('%[^0-9]%', subsrtnumeric+'t') - 1)
FROM (
SELECT subsrtunit = SUBSTRING(string, posofchar, LEN(string)),
subsrtnumeric = SUBSTRING(string, posofnumber, LEN(string))
FROM (
SELECT string, posofchar = PATINDEX('%[^0-9]%', string),
posofnumber = PATINDEX('%[0-9]%', string)
FROM temp
) d
) t
更新版本以处理 99.5 ug \ ml
create TABLE temp
(
string NVARCHAR(50)
)
INSERT INTO temp (string)
VALUES
('4000 ug\ml'),
('2000 ug\ml'),
('%'),
('ug\ml'),
('99.5 ug\ml')
SELECT subsrtunit,LEFT(subsrtnumeric, PATINDEX('%[^0-9.]%', subsrtnumeric+'t') - 1)
FROM (
SELECT subsrtunit = SUBSTRING(string, posofchar, LEN(string)),
subsrtnumeric = SUBSTRING(string, posofnumber, LEN(string))
FROM (
SELECT string, posofchar = PATINDEX('%[^0-9.]%', string),
posofnumber = PATINDEX('%[0-9.]%', string)
FROM temp
) d
) t
更新版本:处理 1 000 ug \ ml,20 000ug \ ml
create TABLE temp
(
string NVARCHAR(50)
)
INSERT INTO temp (string)
VALUES
('4000 ug\ml'),
('2000 ug\ml'),
('%'),
('ug\ml'),
('99.5 ug\ml'),
('1 000 ug\ml'),
('20 000ug\ml')
SELECT substring(replace(subsrtunit,' ',''),PATINDEX('%[0-9.]%', replace(subsrtunit,' ',''))+1,len(subsrtunit)),
LEFT(replace(subsrtnumeric,' ',''), PATINDEX('%[^0-9.]%', replace(subsrtnumeric,' ','')+'t') - 1)
FROM (
SELECT subsrtunit = SUBSTRING(string, posofchar, LEN(string)),
subsrtnumeric = SUBSTRING(string, posofnumber, LEN(string))
FROM (
SELECT string, posofchar = PATINDEX('%[^0-9.]%', replace(string,' ','')),
posofnumber = PATINDEX('%[0-9.]%', replace(string,' ',''))
FROM temp
) d
) t
查看SQLFiddle相同内容。
答案 2 :(得分:1)
这样的事情会起作用吗?根据显示的数据,它看起来会如此。
将其作为选择应用于您的数据集,如果您喜欢结果,则可以从中进行更新。
WITH cte as (SELECT 'ug/mL' ConcUnit, 500 as [Numeric], '' as Unit
UNION ALL SELECT '2000 ug/mL', NULL, '')
SELECT
[ConcUnit] as [ConcUnit],
[Numeric] as [Original Numeric],
[Unit] as [Original Unit],
CASE WHEN ConcUnit LIKE '% %' THEN
SUBSTRING(ConcUnit, 1, CHARINDEX(' ', ConcUnit) - 1)
ELSE [Numeric] END as [New Numeric],
CASE WHEN ConcUnit LIKE '% %'
THEN SUBSTRING(ConcUnit, CHARINDEX(' ', ConcUnit) + 1, LEN(ConcUnit))
ELSE ConcUnit END as [New Unit]
FROM cte
答案 3 :(得分:1)
改变@concunit&amp; @unitx分别
DECLARE @concunit varchar(10)='45.5%'
DECLARE @unitx varchar(10)='%'
BEGIN
SELECT RTRIM(SUBSTRING( @concunit , 1 , CHARINDEX( @unitx , @concunit
) - 1
)) AS Number,
RTRIM(SUBSTRING( @concunit , CHARINDEX( @unitx , @concunit
) , LEN( @concunit
) - (CHARINDEX( @unitx , @concunit
) - 1)
)) AS Unit
end