Share via

Print required numeric data from unstructured string

Krish 81 Reputation points
2020-10-15T13:12:48.81+00:00

Hi everyone,

Can i request you to suggest a way to print the numeric data from a text column which is not followed any format of data. (SQL Server)

Ex: Input Output
123abc ---> 123
123.98abc --> 123.98
123 xy28 --> 123
123_xy_98 -->123
123_98_ab_00 -->123
A_123_123 --> 123
A-123-123 --> 123
A%xy125_123--> 125

Something like above. Input string doesn't follow any particular format. But we always need to take the first numeric part of it (including decimals if we have). We can have underscores, - , % or dot or space or multiple times a special character is repeated.

I have written 7 different SQL case statements , but still missing few more cases and this solution is kind of hard codes one. Can we have anything like dynamically identify the special character and just display the first numeric data including the decimals places(if we have ).

Thanks in advance.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Tom Cooper 8,501 Reputation points
2020-10-15T15:01:26.41+00:00

I added a couple of additional test cases.

Declare @Sample Table(MyData varchar(30));
Insert @Sample(MyData) Values
('123abc'), ---> 123
('123.98abc'), --> 123.98
('123 xy28'), --> 123
('123_xy_98'), -->123
('123_98_ab_00'), -->123
('A_123_123'), --> 123
('A-123-123'), --> 123
('A%xy125_123'), --> 125
('257'),  -- 257
('257ABC'),  -- 257
('abcdef') -- <empty string>;

;
;With cteFirstLoc As
(Select MyData, PatIndex('%[0-9.]%', MyData) As StartPos, Len(MyData) As DataLen
From @Sample),
cteLastLoc As
(Select MyData, StartPos, DataLen, PatIndex('%[^0-9^.]%', SubString(MyData, StartPos,  DataLen)) As EndPos
From cteFirstLoc)
Select MyData, StartPos, EndPos, 
  Case When StartPos = 0 Then ''
    When EndPos = 0 Then SubString(MyData, StartPos,  DataLen)
    Else SubString(MyData, StartPos,  EndPos - 1)
    End As Result
From cteLastLoc;

Tom

Was this answer helpful?

2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.