你可以使用这样的功能
CREATE FUNCTION [dbo].[CheckSentece] (@mainSentence varchar(128), @checkSentence varchar(128))
RETURNS NUMERIC AS
BEGIN
SET @mainSentence=LOWER(@mainSentence)
SET @checkSentence=LOWER(@checkSentence)
DECLARE @pos INT
DECLARE @word varchar(32)
DECLARE @count NUMERIC
SET @count=0
WHILE CHARINDEX(' ', @checkSentence) > 0
BEGIN
SELECT @pos = CHARINDEX(' ', @checkSentence)
SELECT @word = SUBSTRING(@checkSentence, 1, @pos-1)
DECLARE @LEN NUMERIC
//Simple containment check, better to use another charindex loop to check each word from @mainSentence
SET @LEN=(SELECT LEN(REPLACE(@mainSentence,@word,'')))
if (@LEN<LEN(@mainSentence)) SET @count=@count+1
SELECT @checkSentence = SUBSTRING(@checkSentence, @pos+1, LEN(@checkSentence)-@pos)
END
return @count
END
并从第一个句子中包含的第二个句子中获取单词数