Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get numeric values from based on specific substring phrase

I have the alphanumeric text as below

 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 % 

I need to extract numeric values for the left ventricular ejection fraction

Output should be

ID Lowerbound   Upperbound
1   60            65
2   55            60
3   40            50
4   52            NULL 

I have tried below sql syntax searching on character - its failing me for ID 3 and 4(For ID 4,its giving me as 50 but supposed to be 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

Thank you in advance

like image 822
user3594484 Avatar asked Jun 07 '18 17:06

user3594484


People also ask

How do I extract numbers from a string?

The number from a string in javascript can be extracted into an array of numbers by using the match method. This function takes a regular expression as an argument and extracts the number from the string. Regular expression for extracting a number is (/(\d+)/).

How do I extract specific data from a string in Excel?

Depending on where you want to start extraction, use one of these formulas: LEFT function - to extract a substring from the left. RIGHT function - to extract text from the right. MID function - to extract a substring from the middle of a text string, starting at the point you specify.

How do I extract numeric values from a cell?

Select all cells with the source strings. On the Extract tool's pane, select the Extract numbers radio button. Depending on whether you want the results to be formulas or values, select the Insert as formula box or leave it unselected (default).


1 Answers

This will use several CROSS APPLYs to get closer and closer. Some replacements make the string comparable, The first blank from the rear end is the border. The rest is rather easy.

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);
like image 101
Shnugo Avatar answered Oct 30 '22 07:10

Shnugo