我的字母数字文字如下
ID Textfield
1 estimated left ventricular ejection fraction 60-65%
2 estimated left ventricular ejection fraction is 55-60%
3 Left ventricular ejection fraction is approximately 40 to 50%
4 Fractional Short 50 %( 28-48) LV mass 83 gm (<220) systolic function left ventricular ejection fraction = 52 %
我需要提取左心室射血分数的数值
输出应为
ID Lowerbound Upperbound
1 60 65
2 55 60
3 40 50
4 52 NULL
我已尝试过以下sql语法搜索字符 - 它因ID 3和4而失败(对于ID 4,它给我50但应该是52)
SELECT SUBSTRING(textfield,CHARINDEX('-',
textfield)-1,10),dbo.udf_GetNumeric(RIGHT(left(textfield,CHARINDEX('-
',textfield)-1 ),10))AS Lower_bound,left(textfield, CHARINDEX('-',
textfield) )
,dbo.udf_GetNumeric(SUBSTRING(textfield,CHARINDEX('-', textfield)+1,5))
AS Upper_bound
提前谢谢
答案 0 :(得分:0)
从技术上讲,这适用于您的Upperbound问题,至少在您的示例数据的范围内:
CASE WHEN RIGHT(TextField, 4) LIKE '%-[1234567890]' + '%'
THEN Left(Right(TextField,3),2)
WHEN RIGHT(TextField, 6) LIKE '%to [1234567890]%'
THEN Left(Right(TextField,3),2)
WHEN RIGHT(TextField, 6) LIKE '%= [1234567890]%'
THEN Left(Right(TextField,4),2)
END AS UpperBound
但是,你的示例数据非常糟糕,m
答案 1 :(得分:0)
请尝试使用此代码,如果这适用于您的整个数据集,请告知我们。这里的假设是:
1)你的数字总是在“左心室射血分数”字样之后出现。
2)左心室射血分数&#39;之后没有其他数字。除了定义下限和上限的值
3)下限和上限始终从左到右(从小到大)
4)起始表的名称是#temp
{{1}}
答案 2 :(得分:0)
这会使用几个CROSS APPLYs
来越来越近。一些替换使得字符串可比,后端的第一个空白是边框。其余的很容易。
DECLARE @tbl TABLE(ID INT, Textfield VARCHAR(500));
INSERT INTO @tbl VALUES
(1,'estimated left ventricular ejection fraction 60-65%')
,(2,'estimated left ventricular ejection fraction is 55-60%')
,(3,'Left ventricular ejection fraction is approximately 40 to 50%')
,(4,'Fractional Short 50 %( 28-48) LV mass 83 gm (<220) systolic function left ventricular ejection fraction = 52 % ');
SELECT ID
,Rev
,substr
,CASE WHEN hyph>0 THEN LEFT(substr,hyph-1) ELSE substr END AS LowerBound
,CASE WHEN hyph>0 THEN SUBSTRING(substr,hyph+1,10) ELSE NULL END AS UpperBound
FROM @tbl t
CROSS APPLY(SELECT REVERSE(RTRIM(REPLACE(REPLACE(t.Textfield,' to ','-'),' %','%')))) AS A(Rev)
CROSS APPLY(SELECT PATINDEX('% [^1-9]%',A.Rev)) AS B(pos)
CROSS APPLY(SELECT LTRIM(RTRIM(REPLACE(REVERSE(LEFT(A.Rev,B.pos)),'%','')))) AS C(substr)
CROSS APPLY(SELECT CHARINDEX('-',C.substr)) AS D(hyph);