带有连接列的SQL CASE

时间:2013-04-05 17:15:50

标签: sql oracle case

我正在尝试在来自不同列的数据串联中找到子字符串,并根据结果返回一个值。 我试过这个问题:

SELECT
    V_CMR_1.*, 
    T_RICEVITORI.*, 
    CASE 
        WHEN contains(V_CMR_1.SP1C || ' ' || V_CMR_1.SP2C || ' ' || V_CMR_1.SP3C || ' ' || V_CMR_1.SP4C || ' ' || V_CMR_1.SP5C, 'GIALLA') > 0 THEN 'GELBE'
        WHEN contains(V_CMR_1.SP1C || ' ' || V_CMR_1.SP2C || ' ' || V_CMR_1.SP3C || ' ' || V_CMR_1.SP4C || ' ' || V_CMR_1.SP5C, 'ROSSA') > 0 THEN 'ROTE'
        END AS SORTE
FROM 
    MAGAZZINO.V_CMR_1, 
    MAGAZZINO.T_RICEVITORI
WHERE
    V_CMR_1.ID_RICEVITORE=T_RICEVITORI.ID_RICEVITORE 
    AND V_CMR_1.ID_USCITA=:IDUSCITA 
    AND V_CMR_1.ID_POSIZIONE LIKE :ANNO

但是我得到了奇怪的oracle错误(通信通道上的文件结束)。 它会工作吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试使用instr函数代替contains

SELECT V_CMR_1.*, 
       T_RICEVITORI.*, 
       CASE WHEN instr(V_CMR_1.SP1C || ' ' || V_CMR_1.SP2C || ' ' || V_CMR_1.SP3C || ' ' || V_CMR_1.SP4C || ' ' || V_CMR_1.SP5C, 'GIALLA') > 0 THEN 'GELBE'
            WHEN instr(V_CMR_1.SP1C || ' ' || V_CMR_1.SP2C || ' ' || V_CMR_1.SP3C || ' ' || V_CMR_1.SP4C || ' ' || V_CMR_1.SP5C, 'ROSSA') > 0 THEN 'ROTE'
       END AS SORTE
FROM MAGAZZINO.V_CMR_1, MAGAZZINO.T_RICEVITORI
WHERE V_CMR_1.ID_RICEVITORE=T_RICEVITORI.ID_RICEVITORE 
AND V_CMR_1.ID_USCITA=:IDUSCITA AND V_CMR_1.ID_POSIZIONE LIKE :ANNO