你可能分裂和計數:
DECLARE @t NVARCHAR(400)='On August the third the pope talked on vatican square.'
;WITH tally AS
(
SELECT TOP 1000 rn = ROW_NUMBER() OVER(ORDER BY 1/0)
FROM master..spt_values
), cte AS(
SELECT REPLACE(REPLACE(SUBSTRING(' ' + @t + ' ', rn + 1,
CHARINDEX(' ', ',' + @t + ' ', rn + 1) - rn -1),'.', ''), ',','') AS word
FROM tally
WHERE rn <= LEN(' ' + @t + ' ') - 1
AND SUBSTRING(' ' + @t + ' ', rn, 1) = ' '
)
SELECT word, COUNT(*) AS total
FROM cte
GROUP BY word;
LiveDemo
輸出:
╔═════════╦═══════╗
║ word ║ total ║
╠═════════╬═══════╣
║ August ║ 1 ║
║ On ║ 2 ║
║ pope ║ 1 ║
║ square ║ 1 ║
║ talked ║ 1 ║
║ the ║ 2 ║
║ third ║ 1 ║
║ vatican ║ 1 ║
╚═════════╩═══════╝
首先,SQL Server擁有自己的全文搜索*和*數據挖掘服務。你不應該嘗試自己解析文本。其次,有幾十個重複問題和幾個選項來分割一個字符串[如下所示](http://sqlperformance.com/2012/07/t-sql-queries/split-strings) –