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 'China'
Union all select n 'China'
Union all select n 'people of China'
Union all select n 'Japan'
Union all select n 'Japanese'
Union all select n 'someone in my heart'
Union all select n 'people net'
Union all select n 'China is a great country'
Union all select n' we are all Chinese, all Chinese descendants, all dragon descendant'
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 'China ')
Declare @ searchstr nvarchar (20)
Set @ searchstr = n 'China'
Select ID, [value] from TB
Inner join fn_splitstringtorows (@ searchstr) B
On charindex (B. V, A. Value)> 0
Where value like n' % [Chinese] %'
Group by ID, Value
Order by count (distinct V) DESC
Drop table TB
/*
V
----
Medium
Country
Person
(3 data columns are affected)
ID value
----------- Begin ---------------------------------------------------------------------------------------------------------------
2 Chinese
3. Chinese people
9 we are all Chinese, both descendants of the Chinese people and descendants of the Chinese people.
6. Some girls in my heart
1 China
8. China is a great country
5. Japanese
7 People's Network
(8 Data columns are affected)
*/