2

在我的 SQL 中,我正在寻找我正在使用 LIKE 命令搜索的项目。有没有办法确定在每行数据中找到了哪个 LIKE?

SELECT DISTINCT VICSEC.VSID, VICSEC.VSDESCRIPTION
FROM VSUSER.VICSEC INNER JOIN VSUSER.VSNOTES ON VICSEC.VSID=VSNOTES.VSID
WHERE VICSEC.VSSHIPDATE between sysdate and (sysdate+14)
AND(UPPER(VICSEC.VSDESCRIPTION) LIKE '%***GARTER***%'
OR UPPER(VICSEC.VSDESCRIPTION) LIKE '%DEMIBRA%'
OR UPPER(VICSEC.VSDESCRIPTION) LIKE '%PANTY%'
OR UPPER(VICSEC.VSDESCRIPTION) LIKE '%PAJAMA%'
OR UPPER(VICNOTES.VSNOTE) LIKE '%***GARTER***%'
OR UPPER(VICNOTES.VSNOTE) LIKE '%DEMIBRA%'
OR UPPER(VICNOTES.VSNOTE) LIKE '%PANTY%'
OR UPPER(VICNOTES.VSNOTE) LIKE '%PAJAMA%'
OR UPPER(VICSEC.VSSTORITEM) LIKE '%***GARTER***%'
OR UPPER(VICSEC.VSSTORITEM) LIKE '%DEMIBRA%'
OR UPPER(VICSEC.VSSTORITEM) LIKE '%PANTY%'
OR UPPER(VICSEC.VSSTORITEM) LIKE '%PAJAMA%')
ORDER BY VICSEC.VSID
4

1 回答 1

4

您可以CASE WHEN为此使用表达式:

SELECT 
  CASE WHEN UPPER(VICSEC.VSDESCRIPTION) LIKE '%***GARTER***%' THEN 1 else 0 END as GARTER,
  CASE WHEN UPPER(VICSEC.VSDESCRIPTION) LIKE '%DEMIBRA%' THEN 1 else 0 END as DEMIBRA,

ETC...

如果您正在寻找找到其中之一的任何东西,您可以在 WHEN 子句中使用多个表达式,因为它只需要一个表达式作为参数。

CASE WHEN UPPER(VICSEC.VSDESCRIPTION) LIKE '%DEMIBRA%' 
          OR UPPER(VICNOTES.VSNOTE) LIKE '%DEMIBRA%'  
     THEN 1 else 0 END as DEMIBRA,
于 2013-10-18T17:28:55.613 回答