컬럼명 | M_ID | D_N | M_N | DD | E_N | |||
당초값 | 변경된값 | 당초값 | 변경된값 | 당초값 | 당초값 | 변경된값 | ||
셈플데이타 | M1 | ABCDEFG | AFBCDEF | 0100/01 | 0200/01 | 3 | AAA | XXXAAA |
M2 | ABC | ABC | 1102/03 | 1102/03 | 4 | BBB | BBB | |
M3 | ABCDEFGH | AFCDEFG | 0000/00 | 0100/00 | 3 | CCC | XXXCCC | |
M4 | ABCDE | AFBCDE | 0903/03 | 1003/03 | 3 | DDD | XXXDDD | |
M5 | A | AF | 1102/03 | 1202/03 | 3 | BBB | XXXBBB | |
M6 | ABCDEFG | ABCDEFG | 0100/01 | 0100/01 | 5 | AAA | AAA | |
: | : | : | : | : | : | : | : | |
: | : | : | : | : | : | : | : |
위와 같은 데이타 레코드가 약 100개정도 있습니다.
<질문>
DD컬럼이 "3"인 M_ID는
D_N컬럼에서 제일앞 "A"문자 다음에 "F"를 추가하고, 앞(A문자)에서부터 7자리이후는 버린다.
M_N컬럼에서 숫자 앞 두자리에 1을 더한다
E_N컬럼에서 제일 앞에 "XXX"를 추가한다.
이와 같은 쿼리 부탁합니다.
감사합니다.
WITH T AS ( SELECT 'M1' AS M_ID, 'ABCDEFG' AS DN, '0100/01' AS MN, 3 AS DD, 'AAA' AS EN FROM dual UNION ALL SELECT 'M2' AS M_ID, 'ABC' AS DN, '1102/01' AS MN, 4 AS DD, 'BBB' AS EN FROM dual UNION ALL SELECT 'M3' AS M_ID, 'ABCDEFGH' AS DN, '0000/01' AS MN, 3 AS DD, 'CCC' AS EN FROM dual UNION ALL SELECT 'M4' AS M_ID, 'ABCDE' AS DN, '0900/01' AS MN, 3 AS DD, 'DDD' AS EN FROM dual UNION ALL SELECT 'M5' AS M_ID, 'ABC' AS DN, '1100/01' AS MN, 3 AS DD, 'EEE' AS EN FROM dual UNION ALL SELECT 'M6' AS M_ID, 'ABCDEFG' AS DN, '0100/01' AS MN, 5 AS DD, 'FFF' AS EN FROM dual ) SELECT M_ID, CASE WHEN DD = 3 THEN SUBSTR(SUBSTR(DN, 1, 1) || 'F' || SUBSTR(DN, 2), 1, 7) ELSE DN END AS DN, CASE WHEN DD = 3 THEN TO_CHAR(SUBSTR(MN, 1, 2) + 1, 'fm00') || SUBSTR(MN, 3) ELSE MN END AS MN, CASE WHEN DD = 3 THEN 'XXX' || EN ELSE EN END AS EB FROM T A;
필상님 답변갑사합니다.ㅋ
데이타는 입력되어 있다고 할때
답변주신 아래 절은 생략하고,
UPDATE dn, mn, dn
SET dn = SUBSTR(SUBSTR(dn, 1, 1)) || 'F' || SUBSTR(dn, 2), 1, 7),
mn = TO_CHAR(SUBSTR(MN, 1, 2) + 1, 'fm00') || SUBSTR(MN, 3),
en = 'XXX' || EN
WHERE DD = 3
FROM T;
위와 같이 해도 되나요?
--------아래 -------------
WITH T AS (
SELECT 'M1' AS M_ID, 'ABCDEFG' AS DN, '0100/01' AS MN, 3 AS DD, 'AAA' AS EN FROM dual UNION ALL
SELECT 'M2' AS M_ID, 'ABC' AS DN, '1102/01' AS MN, 4 AS DD, 'BBB' AS EN FROM dual UNION ALL
SELECT 'M3' AS M_ID, 'ABCDEFGH' AS DN, '0000/01' AS MN, 3 AS DD, 'CCC' AS EN FROM dual UNION ALL
SELECT 'M4' AS M_ID, 'ABCDE' AS DN, '0900/01' AS MN, 3 AS DD, 'DDD' AS EN FROM dual UNION ALL
SELECT 'M5' AS M_ID, 'ABC' AS DN, '1100/01' AS MN, 3 AS DD, 'EEE' AS EN FROM dual UNION ALL
SELECT 'M6' AS M_ID, 'ABCDEFG' AS DN, '0100/01' AS MN, 5 AS DD, 'FFF' AS EN FROM dual
)
-- Postgresql select M_ID ,case when DD=3 then substr(D_N,1,1)||'F'||substr(D_N,2,5) else D_N end MODIFY_D_N ,case when DD=3 then to_char(to_number(substr(M_N,1,2),'9999')+1,'fm00')||substr(M_N,3,5) else M_N end MODIFY_M_N ,case when DD=3 then 'XXX'||E_N else E_N end MODIFY_E_N from ( select 'M1' M_ID, 'ABCDEFG' D_N, '0100/01' M_N, 3 DD, 'AAA' E_N union all select 'M2', 'ABC', '1102/03', 4, 'BBB' union all select 'M3', 'ABCDEFGH', '0000/00', 3, 'CCC' union all select 'M4', 'ABCDE', '0903/03', 3, 'DDD' union all select 'M5', 'ABC', '1100/01', 3, 'BBB' union all select 'M6', 'ABCDEFG', '0100/01', 5, 'AAA' ) a ;
-- 1. 갱신 대상 확인 SELECT dd , d_n, SUBSTR(d_n, 1, 1) || 'F' || SUBSTR(d_n, 2, 5) AS d_n_new , m_n, LPAD(SUBSTR(m_n, 1, 2) + 1, 2, '0') || SUBSTR(m_n, 3) AS m_n_new , e_n, 'XXX' || e_n AS e_n_new FROM t WHERE dd = 3 ; -- 2. 갱신 UPDATE t SET d_n = SUBSTR(d_n, 1, 1) || 'F' || SUBSTR(d_n, 2, 5) , m_n = LPAD(SUBSTR(m_n, 1, 2) + 1, 2, '0') || SUBSTR(m_n, 3) , e_n = 'XXX' || e_n WHERE dd = 3 ;
안녕하세요 마농님,
답변 주신 내용으로 실행을 해 보았으나
아래와 같은 에러가 발생했는데 수정부탁해도 될까요?
저는 아무리 봐도 문제 될게 없어보이는데 에러가 나네요.ㅠㅠ
감사합니다. 꾸벅^^
UPDATE
t
SET
d_n = SUBSTR(d_n, 1, 1) ||
'F'
|| SUBSTR(d_n, 2, 5)
, m_n = LPAD(SUBSTR(m_n, 1, 2) + 1, 2,
'0'
) || SUBSTR(m_n, 3)
, e_n =
'XXX'
|| e_n
WHERE
dd = 3
;
이 문장을 실행했더니,
"
[Error] Execution (16: 1): ORA-00911: 문자가 부적합합니다"1) 마농님의 원본
SELECT dd
, d_n, SUBSTR(d_n, 1, 1) || 'F' || SUBSTR(d_n, 2, 5) AS d_n_new
, m_n, LPAD(SUBSTR(m_n, 1, 2) + 1, 2, '0') || SUBSTR(m_n, 3) AS m_n_new
, e_n, 'XXX' || e_n AS e_n_new
FROM t
WHERE dd = 3
;
위문장을
2) 붉은색처럼 괄호안에 넣거나
SELECT dd
, d_n(SUBSTR(d_n, 1, 1) || 'F' || SUBSTR(d_n, 2, 5) AS d_n_new)
, m_n(LPAD(SUBSTR(m_n, 1, 2) + 1, 2, '0') || SUBSTR(m_n, 3) AS m_n_new)
, e_n, 'XXX' || e_n AS e_n_new
FROM t
WHERE dd = 3
;
3) 붉은색을 "="처리하면 잘못 된것이겠죠?
SELECT dd
, d_n = SUBSTR(d_n, 1, 1) || 'F' || SUBSTR(d_n, 2, 5) AS d_n_new
, m_n = LPAD(SUBSTR(m_n, 1, 2) + 1, 2, '0') || SUBSTR(m_n, 3) AS m_n_new
, e_n, 'XXX' || e_n AS e_n_new
FROM t
WHERE dd = 3
;
마농님이 답변주신 "1)"번 코드가 맞는것이겠죠?ㅎㅎ