将LIKE结果剥离到它自己的列

时间:2013-10-18 17:25:33

标签: sql oracle

在我的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

1 个答案:

答案 0 :(得分: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,

等...

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

CASE WHEN UPPER(VICSEC.VSDESCRIPTION) LIKE '%DEMIBRA%' 
          OR UPPER(VICNOTES.VSNOTE) LIKE '%DEMIBRA%'  
     THEN 1 else 0 END as DEMIBRA,