2016-02-12 31 views
1

Q1-PatIndex模式正則表達式:如何匹配後跟空格的點? Q2 -PatIndex模式正則表達式:如何匹配一個點後跟兩個空格?PatIndex模式/正則表達式:如何匹配後跟空格的點

我希望把它放在這裏只得到目標內容

Declare @Temp Table(Data VarChar(1000)) 

Insert Into @Temp Values('Lalallaa GOAL: This is the truthmeow. Meow. ') 
Insert Into @Temp Values('Lalallaa GOAL: This is the truth. Meowrwr. ') 
Insert Into @Temp Values('lALALLA GOAL: This is the truth. Meowrwr. NOTINCLUDED: WAWAW') 


Select Left(
      SubString(Data,PATINDEX ('%GOAL%',Data), 8000) , 
      PatIndex('regex here', SubString(Data, PatIndex('%[GOAL]%', Data), 8000))-1) 
From @Temp 

期望輸出

GOAL: This is the truthmeow. 
GOAL: This is the truth. 
GOAL: This is the truth. 

我用Shnugos答案的真實DB,我遇到了一個錯誤:Illegal name character

我檢查了數據類型,它是ntext

+0

'\。\ s'(Q1)或'\。\ S {2}'(Q2)。羅傑出:) – Jan

+1

在這個偉大的野外世界裏有很多教程和在線測試形式。 SO不是作業平臺... – Shnugo

+0

預期產量是多少? –

回答

1

這個怎麼樣:

簡短說明:使用XML標籤更換.將 「分裂」 這個字符串作爲你的字符串中有很多「部分」。的XML值法將採取的第一項的值,該值是字符串直到第一.

Declare @Temp Table(Data VarChar(1000)) 

Insert Into @Temp Values('Lalallaa GOAL: This is the truthmeow. Meow. ') 
Insert Into @Temp Values('Lalallaa GOAL: This is the truth. Meowrwr. ') 
Insert Into @Temp Values('lALALLA GOAL: This is the truth. Meowrwr. NOTINCLUDED: WAWAW') 


Select CAST('<x>' + REPLACE(SubString(Data,PATINDEX ('%GOAL%',Data), 8000),'. ','</x><x>') + '</x>' AS XML).value('x[1]','varchar(max)') 
From @Temp  
+0

我得到了Lalallaa GOAL :這是真相,Lalallaa目標:這是事實,LALALLA目標:這是事實 –

+0

@PhilipMorris,這很容易,只需使用你自己的'子串'方法來切斷開始,編輯我的答案...... – Shnugo

+0

@菲利普莫里斯,如果你最後需要點,只需添加它... – Shnugo

1

您也可以嘗試使用LIKE語句。

columnname LIKE '. %' 

編輯:

試試這個:

Select SUBSTRING(data, LEN(LEFT(data, CHARINDEX ('GOAL:', data))) , LEN(data) - LEN(LEFT(data, CHARINDEX ('GOAL:', data))) - LEN(RIGHT(data, LEN(data) - CHARINDEX ('.', data))) - 1) 
From @Temp 
+0

我得到了目標:這是真相。,目標:這是真相,目標:這是真跡 –