パラメータが特定の用語で始まるかどうか、またはその用語を含むが、その用語で始まらないかどうかをテストする SQL 関数を作成しようとしています。
基本的に、パラメータが単語で始まる場合、関数は 0 を返します。それ以外の場合は 1 を返します。
これは私が見つけた別の関数から適応しようとしている関数の骨です:
CREATE FUNCTION [dbo].[fnGetRelevance]
(
@fieldName nvarchar(50),
@searchTerm nvarchar(50)
)
RETURNS @value int -- this is showing an error, it seems to expect table but all I want is an int
(
-- does this need to be here? If so, what should it be?
)
AS
BEGIN
declare @field TABLE(Data nvarchar(50))
insert into @field
select Data from @fieldName
if (Data like @searchTerm + '%') -- starts with
begin
return 0
end
else if (Data like '%' + @searchTerm + '%' and Data not like @searchTerm + '%') -- contains, but doesn't start with
begin
return 1
end
END
GO