1

我想对名字、中间名和姓氏进行组合搜索。查询是:

SELECT  picture,person_id,   firstname+ ' ' +middlename+ ' ' +lastname AS fullName
FROM    PERSON
WHERE   (firstname+ ' ' +middlename+ ' ' +lastname LIKE"%#queryString#%")

现在这适用于:Peter den Test (first/middle/last) 这不适用于:Marc Baker (first/no middle name/last)。在马克贝克出现之前,我必须输入两个空格。我可以检查查询中的中间名是否为空吗?

谢谢!

4

3 回答 3

1

是的,您可以使用它CASE来进行检查,例如:

SELECT  picture,person_id,   firstname+ ' ' +middlename+ ' ' +lastname AS fullName
FROM    PERSON
WHERE   (firstname
          + CASE WHEN middlename = '' OR middlename IS NULL THEN '' ELSE ' ' +middlename END 
          + ' ' +lastname LIKE"%#queryString#%")
于 2013-10-09T09:12:59.843 回答
1

我认为这会起作用:

WHERE   (firstname+ ' ' +LTRIM(middlename+ ' ' +lastname) LIKE"%#queryString#%")
于 2013-10-09T09:11:54.583 回答
0

试试这样:

SELECT  picture,person_id,   firstname+ ' ' +middlename+ ' ' +lastname AS fullName
FROM    PERSON
WHERE   (firstname+ ' ' + LTRIM(middlename + ' ') + lastname LIKE"%#queryString#%")
于 2013-10-09T09:13:22.630 回答