I have data like this:
String 1: 'Random Text 3 Random 568 Text 5.5 Test Random Text 345'
String 2: 'Random Text 3 Test Text Random'
String 3: 'Random Text 777 Random Text'
The output I expect is:
String 1: '5.5'
String 2: '3'
String 3: Nothing should output
The numbers that should be output always appear before the word Test.
I would like the SQL to be compatible with Microsoft SSMS.
4条答案
按热度按时间c9x0cxw01#
Please try the following solution.
It is using tokenization via XML and XQuery.
Notable points:
CROSS APPLY
clause creates XML for each row.XML sample for ID=1
SQL
Ouput
dvtswwa32#
Just another option using a bit of
JSON
and the window functionlead() over()
Example
Results
eanckbw93#
Given that the pattern (with the numbers before 'test' is consistent across all the string, here is the sql for extracting the number:
Hope this helped!
jvlzgdj94#
You need to create a Scalar Function like this (I did this in SQL Server)
You use like this select dbo.GetNumberBeforeStringTest()
e.g. select dbo.GetNumberBeforeStringTest('Random Text 3 Random 568 Text 5.5 Test Random Text 345')
Result: 5.5