我有類似的需要解析出一組參數存儲在一個IIS日誌'csUriQuery字段,看起來像這樣:id=3598308&user=AD\user¶meter=1&listing=No
this format需要。
我結束了創建一個用戶自定義函數來完成之間的字符串,以下假設:
- 如果找不到起始發生,一個
NULL
返回,並
- 如果結局發生未找到,該字符串的其餘部分返回
下面的代碼:
CREATE FUNCTION dbo.str_between(@col varchar(max), @start varchar(50), @end varchar(50))
RETURNS varchar(max)
WITH EXECUTE AS CALLER
AS
BEGIN
RETURN substring(@col, charindex(@start, @col) + len(@start),
isnull(nullif(charindex(@end, stuff(@col, 1, charindex(@start, @col)-1, '')),0),
len(stuff(@col, 1, charindex(@start, @col)-1, ''))+1) - len(@start)-1);
END;
GO
針對上述問題,用法如下:
DECLARE @a VARCHAR(MAX) = 'All I knew was that the dog had been very bad and required harsh punishment immediately regardless of what anyone else thought.'
SELECT dbo.str_between(@a, 'the dog', 'immediately')
-- Yields' had been very bad and required harsh punishment '
你得到什麼錯誤,或者是什麼回來? – eatonphil
「狗已經非常糟糕,需要立即勒索苛刻的懲罰」 –