| 目次 |
|---|
|
・case ・case条件で検索条件を変更 |
case 比較対象フィールド when 比較値1 then 代替値1
[when 比較値2 then 代替値2]
.....
[else デフォルト値]
end
使用方法は
select
case 3 when 3 then 'three'
when 4 then 'four'
end as case
from dual;
というように書きます。すると
case
--------
three
というように比較対象フィールドである3とマッチする比較値の代替値であるthreeが返ってきます。
select
case 5 when 3 then 'three'
when 4 then 'four'
else 'none'
end as case
from dual;
case
----
none
また比較対象フィールドがいずれの比較値とも等しくない場合で、else文を省略している場合はnullが返ってきます。
select
case 5 when 3 then 'three'
when 4 then 'four'
end as case
from dual;
case
----
null
SELECT *
FROM TABLE
WHERE
(CASE WHEN FLG = '1' THEN HINBAN
WHEN FLG = '0' THEN HEN_HINBAN
END
) LIKE 'SB_%'
というようにFLGが1ならHINBAN LIKE 'SB_%'、FLGが0ならHEN_HINBAN LIKE 'SB_%'というように検索条件を変更することが可能です。