在SQL中,我試圖在特定單詞後返回第一組數值。我只想要特定單詞後的字符串中的數字。 如:「你好:」SQL - 如何在特定單詞後返回一組數字
例如:
hello : 123 should return 123
hello : x123 should return 123
hello : 123 456 should return 123
cake : 10 should not return anything
到目前爲止,我已經工作了,我需要做類似的東西 -
Declare @sInitialString varchar(max)
@sInitialString = " hello hello : 123 456"
--FIND ' hello : ' within @sInitialString
-- possibly save this a new substring or string?
-- find the first numerical value after this has been found
看似簡單,但是從以前的帖子,似乎更復雜。
我設法讓所有的數值返回
DECLARE @sInitialString VARCHAR(MAX)
SET @sInitialString = (SELECT UPPER(' hello hello : 123 '))
select substring(@sInitialString,patindex('%[0-9]%',@sInitialString),100)
我剛纔似乎在我的方法或解決方案無論是失去了一些東西。有沒有人設法實現這一目標?
我想知道這個操作背後的原因是什麼。 – athabaska
也許應該把這個邏輯放到你的客戶端,會更容易 – athabaska
最好的選擇是把這個邏輯封裝到SQL CLR函數中。在函數內部,您可以使用正則表達式或使用字符串拆分操作來實現此邏輯。 –