[MariaDB]nvl, nvl2, with
■ 회원번호의 Max를 구하고, 다음 회원번호 구하는 과정.
SQL > select max(clt_id) from tb_client;
결과 > C01006
SQL > select concat('C',lpad(substr(max(clt_id),2) + 1,5,'0')) from tb_client;
결과 > C01007
■ With 구분으로 과정을 정리
SQL >
with vi_x as (
select max(clt_id) as clt_id from tb_client
)
select clt_id
, substr(clt_id,2) as clt_id5
, substr(clt_id,2) + 1 as clt_id5_1
, lpad(substr(clt_id,2) + 1,5,'0') as clt_id_lpad
, concat('C',lpad(substr(clt_id,2) + 1,5,'0')) as clt_id_next
from vi_x
;
결과 > C01006 01006 1007 01007 C01007
■ nvl, nvl2 테스트(10.1 이상부터 Oracle 호환모드로 제공되는 거 같음.)
SQL >
with vi_x as (
select null as c1
, 'x' as c2
)
select nvl (c1,'Null이다') as c1
, nvl2(c1,'Null아니다','Null이다') as c1_nvl2
, nvl2(c2,'Null아니다','Null이다') as c1_nvl2
from vi_x
;
결과 : Null이다 Null이다 Null아니다