Please consider the below table. I am trying to retrieve only the EUR amount within the Tax
strings. Some records vary more than the other in size, but the float numbers are always there.
OrderID SKU Price Tax
**** **** **** [<TV<standard#21.0#false#21.36#EUR>VT>]
**** **** **** [<TV<standard#21.0#false#7.21#EUR>VT>]
**** **** **** [<TV<standard#17.0#false#5.17#EUR>VT>]
I wrote a regular expression that matches what I need: \d+\W\d+
returns me both float values within the string. In Oracle SQL I can simply get the second occurrence with a query like:
SELECT REGEXP_SUBSTR(column, '\d+\W\d+',1,2) FROM table
Using the above approach I retrieve 21.36
, 7.21
and 5.17
for those three records.
How can I achieve this with SQL Server?
Obviously regex would be the likely tool of choice here. But SQL Server does not have much native regex support. Here is a pure SQL Server solution making use of PATINDEX
and CHARINDEX
. It is a bit verbose, but gets the job done:
SELECT
SUBSTRING(Tax,
CHARINDEX('#', Tax, PATINDEX('%[0-9]#%', Tax) + 3) + 1,
CHARINDEX('#', Tax, CHARINDEX('#', Tax, PATINDEX('%[0-9]#%', Tax) + 3) + 1) -
CHARINDEX('#', Tax, PATINDEX('%[0-9]#%', Tax) + 3) - 1)
FROM yourTable;