sql 判斷 某列中是否包含 某種符號

海乐学习發表於2024-06-25

語法:

SELECT CASE
    WHEN INSTR(your_column, '-') > 0 THEN '包含減號'
    ELSE '不包含減號'
END AS contains_hyphen
FROM your_table;

示例一:

判斷 sessionid 列中 是否 包含 “ - ” 號,當包含則 去掉 “ - ” 號之後的資料,若不包含則顯示 原 sessionid 列的資料

SELECT sessionid  , 
  CASE
    WHEN INSTR(sessionid, '-') > 0 THEN SUBSTR(sessionid, 1, INSTR(sessionid, '-') - 1)
    ELSE sessionid
  END as sessionidnew
FROM cti_cdr  
where timestart<='2024-06-25 16:50:00' and timestart>='2024-06-13 16:15:00' 

示例二:

需要用 這個查詢出來的新列 sessionidnew 當組合查詢和條件時

SELECT cti_satisfaction.callid,  cti_satisfaction.surveyresult,cti_cdr.recording,cti_cdr.sessionid  
FROM cti_cdr INNER JOIN cti_satisfaction  ON  
  CASE
    WHEN INSTR(cti_cdr.sessionid, '-') > 0 THEN SUBSTR(cti_cdr.sessionid, 1, INSTR(cti_cdr.sessionid, '-') - 1)
    ELSE cti_cdr.sessionid
  END   = cti_satisfaction.callid 
where cti_cdr.timestart<='2024-06-25 16:50:00' and cti_cdr.timestart>='2024-06-13 16:15:00' 

相關文章