militala 2019-04-02
代码如下:
IF OBJECT_ID('TB')IS NOT NULL DROP TABLE TB GO CREATE TABLE tb (ID INT IDENTITY(1,1),VALUE NVARCHAR(100)) INSERT tb SELECT N'中国' UNION ALL SELECT N'中国人' UNION ALL SELECT N'中国人民' UNION ALL SELECT N'日本' UNION ALL SELECT N'日本人' UNION ALL SELECT N'我的心中有人姑娘' UNION ALL SELECT N'人民网' UNION ALL SELECT N'中国是个伟大的国家' UNION ALL SELECT N'我们都是中国人,都是炎黄子孙,都是龙人传人' IF OBJECT_ID('fn_SplitStringToROWS')IS NOT NULL DROP FUNCTION fn_SplitStringToROWS GO CREATE FUNCTION fn_SplitStringToROWS ( @str NVARCHAR(100) ) RETURNS @t TABLE(v NVARCHAR(2)) AS BEGIN DECLARE @i INT SET @i=1 WHILE @i<=LEN(@str) BEGIN INSERT @t SELECT SUBSTRING(@str,@i,1) SET @i=@i+1 END RETURN END GO SELECT * FROM DBO.fn_SplitStringToROWS(N'中国人') DECLARE @searchSTR NVARCHAR(20) SET @searchSTR=N'中国人' SELECT ID,[VALUE] FROM tb a INNER JOIN fn_SplitStringToROWS(@searchSTR) b ON CHARINDEX(b.v,a.VALUE)>0 WHERE VALUE LIKE N'%[中国人]%' GROUP BY ID,VALUE ORDER BY COUNT(DISTINCT v) DESC DROP TABLE tb