SQL 如果两个字符串共享任何字符则为 TRUE

SQL Get TRUE if two strings share any character

我想知道,是否有获得两个字符串比较的 boolean/bit 结果的技巧,其中 TRUE 值将 returned 如果两个字符串之间存在共同字符

一个字符串是授予访问权限的用户列表(即 "OTV",存储在用户配置文件中),另一个是有权访问对象的组列表(即 "MDSKTUQ",存储在数据库中的记录中)。每个字符都是一个用户组的名称。

在真正的 SQL 查询中,我想要 return 行,其中包含授予用户的任何组。类似于:

-- Following should display all files attached to record 145
-- to which has the user granted access:
SELECT FileCaption, FileName, FileType, Category FROM DocFiles 
WHERE RecordID = 145 AND CONTAINS(GroupsRead, <<list_of_groups_string>>)

我有一个客户端解决方案,但可行的SQL端解决方案会更整洁、更安全。 欢迎评论和讨论。

此致,

橡木

给你 - Pressie!

ALTER FUNCTION StringContainsCharactersFrom 
(
    -- Add the parameters for the function here
    @source varchar(100),
    @candidate varchar(100)
)
RETURNS bit
AS
BEGIN

    declare @candidatePos int = 1;
    while (@candidatePos <= len(@candidate))
    BEGIN

        declare @candidateLetter varchar = substring(@candidate, @candidatePos, 1);
        if (CHARINDEX(@candidateLetter,@source, 1) > 0)
            return 1;
        set @candidatePos = @candidatePos + 1;
    END
    return 0;

END
GO