호출 예시
SELECT case_when('1', '1', 'A'::CHAR, 'aa'::CHAR);
SELECT case_when('1', '2', 'A'::CHAR, NULL);
정의
-- ---------------------------------------------------------------------------------------------
-- CASE ~ WHEN ~ THEN ~ END 래핑하는 편의성 함수
-- 예: SELECT case_when('1', '2', 'A'::CHAR, 'aa'::CHAR);
-- 예: SELECT case_when('1', '2', 'A'::CHAR, NULL);
-- ---------------------------------------------------------------------------------------------
DROP FUNCTION IF EXISTS case_when;
CREATE FUNCTION case_when(
cond1 VARCHAR,
cond2 VARCHAR,
if_data ANYELEMENT,
else_data ANYELEMENT
) RETURNS ANYELEMENT AS $$
BEGIN
IF cond1 = cond2 THEN
RETURN if_data;
ELSE
RETURN else_data;
END IF;
END;
$$ LANGUAGE plpgsql;
Befor/After
-- BEFORE
CASE
WHEN S.open_yn = 'Y'
THEN P.post_idx
ELSE NULL
END AS post_idx,
-- AFTER
case_when(S.open_yn, 'Y', P.post_idx::INT, NULL);