Home > Mobile >  SQL Search for words from one sentence in another sentence
SQL Search for words from one sentence in another sentence

Time:11-04

In SQL Server, I have two strings. Need to check if string1 has any matching words in string2. It should exactly match at least a word in the sentence.

String2: Hello String How Are You

Match Scenario: String1: Hello How Am I String1

Hello is common in both strings

No match Scenario: String1: Am I String1

No Word is common in the strings

CodePudding user response:

Similar to your last question, we just join the results of string_split()

declare @string2 varchar(max) = 'Hello String How Are You';
declare @string1 varchar(max) = 'Hello How Am I String1';

Select A.Value
 From string_split(@string2,' ') A
 Join string_split(@string1,' ') B
   on A.value=B.value
 Group By A.value     

Results

Value
Hello
How
  • Related