In this post Extract only numbers from a String, I described a method of extrating numbers from a string using split and concatenate technique
In this post Regular expression, I described a method to extract whole numbers if a specific digit is found Consider that you have a situation where you need to extract only amounts from a text Here is a way to do it Consider this example
declare @sample table ( data varchar(200) ) insert @sample select 'The amount is $87623.63' union all select '$87.21 is paid' union all select 'you have credit of 1000 US Dollors thanks' select data, substring ( data, patindex('%[0-9]%',data), datalength(data)- patindex('%[0-9]%', reverse(data))- patindex('%[0-9]%',data)+2 ) as amount from @sample where data like '%[0-9]%'
The Output is
data amount ---------------------------------- --------------- The amount is $87623.63 87623.63 $87.21 is paid 87.21 you have credit of 1000 US Dollors thanks 1000
Tags: t-sql, sql_server, extract_number, parsing, regular_expression,
Hi Madhivanan,
can you please elaborate on why 2 is being added?
Thanks, Sreelekha
Sreelekha, The code finds the position of a number from both end (lef and right) so 2 should be added to include those digits