ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄/DataBase

[SQL] 였라클 λ‚΄μž₯ ν•¨μˆ˜ - ν˜• λ³€ν™˜ ν•¨μˆ˜ ( 단일행 ν•¨μˆ˜ )

NaNaRinπŸ™ƒ 2021. 3. 2. 15:50

1. ν˜• λ³€ν™˜ ν•¨μˆ˜λž€

- μ˜€λΌν΄μ—μ„œ μ œκ³΅ν•˜λŠ” μžλ£Œν˜•μ„ ν•„μš”μ— 따라 λ°”κΏ” μ£Όμ–΄μ•Ό ν•  λ•Œ μ§€μ •λœ μžλ£Œν˜•μ„ λ°”κΏ”μ£ΌλŠ” ν•¨μˆ˜

- μ•”μ‹œμ  ν˜• λ³€ν™˜(implicit type conversion) : μžλ™ ν˜• λ³€ν™˜. μˆ«μžλ‘œ 인식 κ°€λŠ₯ν•œ 문자 데이터가 μžλ™μœΌλ‘œ 숫자둜 바뀐 ν›„ μ—°μ‚° μˆ˜ν–‰

- λͺ…μ‹œμ  ν˜• λ³€ν™˜ (explicit type conversion) : ν˜• λ³€ν™˜ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ 직접 μžλ£Œν˜•μ„ λ³€ν™˜ν•΄ μ£ΌλŠ” 방식 

 

2. λ‚ μ§œ, 숫자 데이터λ₯Ό 문제 λ°μ΄ν„°λ‘œ λ³€ν™˜ν•˜λŠ” TO_CHAR ν•¨μˆ˜

- 'NLS_DATE_LANGUAGE = language' 의 languageμ—λŠ” KOREAN, ENGLISH, JAPANESE 등이 κ°€λŠ₯

TO_CHAR([λ‚ μ§œ 데이터], [좜λ ₯되길 μ›ν•˜λŠ” 문자 ν˜•νƒœ])
TO_CHAR([λ‚ μ§œ 데이터], [좜λ ₯되길 μ›ν•˜λŠ” 문자 ν˜•νƒœ], 'NLS_DATE_LANGUAGE = [language]'(선택))

-- 0. νŠΉμ • 언어에 맞좰 좜λ ₯
SELECT  TO_CHAR(SYSDATE, 'MM') AS MM,	    
        TO_CHAR(SYSDATE, 'MON', 'NLS_DATE_LANGUAGE = KOREAN') AS MON_KOR,
        TO_CHAR(SYSDATE, 'MONTH', 'NLS_DATE_LANGUAGE = KOREAN') AS MONTH_KOR,
        TO_CHAR(SYSDATE, 'MON', 'NLS_DATE_LANGUAGE = ENGLISH') AS MON_ENG,
        TO_CHAR(SYSDATE, 'MONTH', 'NLS_DATE_LANGUAGE = ENGLISH') AS MONTH_ENG,
        TO_CHAR(SYSDATE, 'MON', 'NLS_DATE_LANGUAGE = JAPANESE') AS MON_JPN,
        TO_CHAR(SYSDATE, 'MONTH', 'NLS_DATE_LANGUAGE = JAPANESE') AS MONTH_JPN,
FROM DUAL;

 

 

-- 1. λ‚ μ§œ ν‘œν˜„ ν˜•μ‹
SELECT  SYSDATE,							TO_CHAR(SYSDATE, 'CC') AS CC,
		TO_CHAR(SYSDATE, 'YYYY') AS YYYY,	TO_CHAR(SYSDATE, 'YY') AS YY,
        TO_CHAR(SYSDATE, 'MM') AS MM,	    TO_CHAR(SYSDATE, 'MON') AS MON,
        TO_CHAR(SYSDATE, 'MONTH') AS MONTH,	TO_CHAR(SYSDATE, 'DD') AS DD,
        TO_CHAR(SYSDATE, 'DDD') AS DDD,     TO_CHAR(SYSDATE, 'DY') AS DY,
        TO_CHAR(SYSDATE, 'DAY') AS DAY,     TO_CHAR(SYSDATE, 'W') AS W
FROM DUAL;

 

-- 2. μ‹œκ°„ ν‘œν˜„ ν˜•μ‹
SELECT  SYSDATE,
        TO_CHAR(SYSDATE, 'HH24:MI:SS') AS HH24MISS,
        TO_CHAR(SYSDATE, 'HH12:MI:SS') AS HH12MISS,
        TO_CHAR(SYSDATE, 'HH:MI:SS AM') AS HHMISS_AM,
        TO_CHAR(SYSDATE, 'HH:MI:SS PM') AS HHMISS_PM,
        TO_CHAR(SYSDATE, 'HH:MI:SS A.M.') AS HHMISS_AM2
FROM DUAL;

 

 

-- 3. 숫자 데이터 ν‘œν˜„ ν˜•μ‹
SELECT      TO_CHAR(123456, '$9,999,999') AS NUM9_$,
            TO_CHAR(123456, 'L9,999,999') AS NUM9_L,
            TO_CHAR(123456, '9,999,999.99') AS NUM9_1,
            TO_CHAR(123456, '$0,000,000') AS NUM0_$,
            TO_CHAR(123456, 'L0,000,000') AS NUM0_L,
            TO_CHAR(123456, '0,000,000.00') AS NUM0_1
FROM DUAL;

 

3. 문자 데이터λ₯Ό 숫자 λ°μ΄ν„°λ‘œ λ³€ν™˜ν•˜λŠ” TO_NUMBER ν•¨μˆ˜

- λ¬Έμžμ—΄μ„ μ§€μ •λœ ν˜•νƒœμ˜ 숫자 λ°μ΄ν„°λ‘œ λ³€ν™˜

- 숫자 데이터가 κ°€κ³΅λœ 문자 λ°μ΄ν„°λ‘œ μ €μž₯λ˜μ–΄ 있고 κ·Έ 데이터λ₯Ό μ‚°μˆ  연산에 μ‚¬μš©ν•˜κ³ μž ν•  경우 μ‚¬μš©

TO_NUMBER([λ¬Έμžμ—΄ 데이터], [인식될 숫자 ν˜•νƒœ])

-- 1.
SELECT  TO_NUMBER('1,500', '999,999') - TO_NUMBER('1,300', '999,999')
FROM 	DUAL;

  (1) TO_NUMBER('1,500', '999,999') - TO_NUMBER('1,300', '999,999') : 숫자 1500κ³Ό 1300 이 κ°€κ³΅λ˜μ–΄ 계산이 λΆˆκ°€λŠ₯ν•΄ 숫자 λ°μ΄ν„°λ‘œ λ³€ν™˜ν•˜μ—¬ κ³„μ‚°ν•œλ‹€. κ²°κ³Όκ°’ 200

 

4. 문자 데이터λ₯Ό λ‚ μ§œ λ°μ΄ν„°λ‘œ λ³€ν™˜ν•˜λŠ” TO_DATE ν•¨μˆ˜

- λ¬Έμžμ—΄μ„ μ§€μ •λœ ν˜•νƒœμ˜ λ‚ μ§œ λ°μ΄ν„°λ‘œ λ³€ν™˜

TO_DATE([λ¬Έμžμ—΄ 데이터], [인식될 λ‚ μ§œ ν˜•νƒœ])

-- 1. 2.
SELECT 	TO_DATE('2021/03/02', 'YYYY-MM-DD') AS TODATE1,
		TO_DATE('20210302', 'YYYY-MM-DD') AS TODATE2
FROM 	DUAL;

  (1) (2) : 2021-03-21 둜 λ³€ν™˜λ¨