如何转换管道符号'||'进入mssql

时间:2013-09-30 11:35:52

标签: sql-server

SELECT COUNT(*) AS CNT
FROM ABCD
WHERE TRIM(UPPER(FIRSTNAME)||UPPER(SECONDNAME)) LIKE UPPER('aa')||UPPER('aaa') 

我需要将上面的查询转换为MSSQL。请帮助。

2 个答案:

答案 0 :(得分:1)

我认为你正试图连接。使用+运算符或CONCAT函数。

WHERE LTRIM(RTRIM(UPPER(FIRSTNAME) + UPPER(SECONDNAME))) LIKE '%' + UPPER('aa') + UPPER('aaa') + '%'

WHERE LTRIM(RTRIM(CONCAT(UPPER(FIRSTNAME), UPPER(SECONDNAME)))) LIKE CONCAT('%', UPPER('aa'), UPPER('aaa'), '%')

答案 1 :(得分:0)

正确的语法是:

SELECT COUNT(*) AS CNT
FROM Production.Categories
WHERE (LTRIM(RTRIM(UPPER(FIRSTNAME))) LIKE '%' + UPPER('aa') + '%'
    OR LTRIM(RTRIM(UPPER(FIRSTNAME))) LIKE '%' + UPPER('aaa') + '%')
OR
  (LTRIM(RTRIM(UPPER(SECONDNAME))) LIKE '%' + UPPER('aa') + '%'
    OR LTRIM(RTRIM(UPPER(SECONDNAME))) LIKE '%' + UPPER('aaa') + '%'))

但是,当你的名字或第二名包含'aaa'时,那么肯定包含'aa',所以如果你这样写,你的查询就会得到满足:

SELECT COUNT(*) AS CNT
FROM Production.Categories
WHERE 
    LTRIM(RTRIM(UPPER(FIRSTNAME))) LIKE '%' + UPPER('aa') + '%'
OR
    LTRIM(RTRIM(UPPER(SECONDNAME))) LIKE '%' + UPPER('aa') + '%'