我正在尝试创建一个 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