2016-07-24 84 views
0

我有一個帶有多個句子的文本塊,我想分解它們並在一個新行上顯示每個句子。我嘗試過使用CHARINDEX和SUBSTRING,但是一旦你通過了第二句話,代碼變得非常複雜並且很難重複。在這裏,我得到了兩個放棄了實現的代碼是要迅速滾雪球:如何將一段文本分成SQL服務器中的單個句子?

DECLARE @TEXT NVARCHAR(MAX) = 'Has many applications. The price is low. The quality is good. Availability is widespread.' 
DECLARE @TEXTLine1 NVARCHAR(MAX) = LEFT(@TEXT,CHARINDEX('.',@TEXT)) 
DECLARE @TEXTLine2 NVARCHAR(MAX) = SUBSTRING(@TEXT,CHARINDEX('.',@TEXT)+2,CHARINDEX('.',SUBSTRING(@TEXT,CHARINDEX('.',@TEXT)+2,50))) 
PRINT @TEXTLine1 
PRINT @TEXTLine2 

正如你所看到的,我基礎上,全面停止分割句子。有沒有辦法告訴SUBSTRING找到角色的第n個實例?這將使任務變得簡單。

回答

0

藉助解析函數

Declare @TEXT VarChar(max) = 'Price if $15.25 is NOT split. Has many applications. The price is low. The quality is good. Availability is widespread.' 

Select * from [dbo].[udf-Str-Parse](@Text,'. ') 

返回

Key_PS Key_Value 
1  Price if $15.25 is NOT split 
2  Has many applications 
3  The price is low 
4  The quality is good 
5  Availability is widespread. 

的UDF

CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@delimeter varchar(10)) 
--Usage: Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',') 
--  Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ') 
--  Select * from [dbo].[udf-Str-Parse]('id26,id46|id658,id967','|') 

Returns @ReturnTable Table (Key_PS int IDENTITY(1,1) NOT NULL , Key_Value varchar(max)) 

As 

Begin 
    Declare @intPos int,@SubStr varchar(max) 
    Set @IntPos = CharIndex(@delimeter, @String) 
    Set @String = Replace(@String,@[email protected],@delimeter) 
    While @IntPos > 0 
     Begin 
     Set @SubStr = Substring(@String, 0, @IntPos) 
     Insert into @ReturnTable (Key_Value) values (@SubStr) 
     Set @String = Replace(@String, @SubStr + @delimeter, '') 
     Set @IntPos = CharIndex(@delimeter, @String) 
     End 
    Insert into @ReturnTable (Key_Value) values (@String) 
    Return 
End 
+0

完美!我沒有想過小數位。所有我必須添加的是:IIF(右(Key_value,1)='。',Key_value,Key_value +'。')以確保所有行在結尾處回到完全停止狀態。 – SteelyDanFan

+0

樂意幫忙。我過去曾被那條狗咬過。乾杯。 –

+0

今天玩了這個 - 行'Set @String = Replace(@ String,@ delimeter + @ delimeter,@ delimeter)'是做什麼的? – SteelyDanFan

0

在考慮我的問題,我想出了這個 - 這似乎有點大材小用,但我想不出任何其他方式:

DECLARE @text NVARCHAR(MAX) = 'Has many applications. The price is low. The quality is good. Availability is widespread.'; --Set text. 
DECLARE @text2 NVARCHAR(MAX) = LEFT(@text,CHARINDEX('.',@text)); --Extract first sentence. 

PRINT @text2; --Print first sentence. 

SET @text = RIGHT(@text,LEN(@text)-LEN(@text2)); --Subtract @text2 from @text - will include the space at the begining that was after the first full stop. 

WHILE LEN(@text) >0 
BEGIN 
SET @text = RIGHT(@text, LEN(@text)-1);--Take of the space that after the full stop in previous iteration of @text. 
SET @text2 = LEFT(@text,CHARINDEX('.',@text));--Exract the 'new' first sentence. 
PRINT @text2; 
SET @text = RIGHT(@text,LEN(@text)-LEN(@text2)); --Subtract @text2 from @text - will include the space at the begining that was after the first full stop. 
END; 

其他建議都歡迎。

編輯 - John Cappelletti的出色答案促使我改善自己。它現在不會被小數點抓住,也可以識別回車。將有一個不同的應用程序,但認爲包括任何尋找任何解決方案的任何人可能是有用的。

DECLARE @text NVARCHAR(MAX) = 
'I would like to pay £22.99. Has many applications. 
£45.00 is good value. The price is low. The quality is good. 
Availability is widespread. Good value at £5.00.' --Set text. 
DECLARE @text2 NVARCHAR(MAX) = LEFT(@text,CHARINDEX('. ',@text)) --Extract first sentence. 


PRINT @text2 --Print first sentence. 
SET @text = RIGHT(@text,LEN(@text)-LEN(@text2)) --Subtract @text2 from @text - will include the space at the begining that was after the first full stop. 
WHILE LEN(@text) >0 
BEGIN 
SET @text = RIGHT(@text, LEN(@text)-1)--Take off the space that after the full stop in previous iteration of @text. 
SET @text2 = IIF(LEN(LEFT(@text,CHARINDEX('. ',@text)))=0, LEFT(@text,CHARINDEX('.',@text)+LEN(RIGHT(@text, LEN(@text)-LEN(LEFT(@text,CHARINDEX('.',@text)))))),LEFT(@text,CHARINDEX('. ',@text)))--Extract the new first sentence. 
PRINT @text2 
SET @text = RIGHT(@text,LEN(@text)-LEN(@text2)) --Subtract @text2 from @text - will include the space at the begining that was after the first full stop. 
END 
2

使用從here分割字符串函數的..

那將是很容易做到這樣的..

DECLARE @TEXT NVARCHAR(MAX) = 'Has many applications. The price is low. The quality is good. Availability is widespread.' 

select * from 
[dbo].[SplitStrings_Numbers](@text,'.') 

輸出:

Item 
Has many applications 
The price is low 
The quality is good 
Availability is widespread