Oracle #7-4 - DQL ν˜•λ³€ν™˜ FUNCTION

ennakoidaΒ·2023λ…„ 6μ›” 22일
0

Oracle

λͺ©λ‘ 보기
10/13

πŸ“ ν˜•λ³€ν™˜ ν•¨μˆ˜

TO_CHAR, TO_DATE, TO_NUMBER

TO_CHAR

SELECT EMP_NAME
     , TO_CHAR(HIRE_DATE, 'YYYY-MM-DD')
     , TO_CHAR(HIRE_DATE, 'YY/MON, DAY, DY')
FROM EMPLOYEE;

TO_CHARλŠ” λ‚ μ§œν˜• ν˜Ήμ€ μˆ«μžν˜•μ„ λ¬Έμžν˜•μœΌλ‘œ λ³€ν™˜ν•œλ‹€.

SELECT EMP_NAME
     , TO_CHAR(SALARY, 'L999,999,999')
     , TO_CHAR(SALARY, '000,000,000')
FROM EMPLOYEE;

9둜 ν‘œν˜„ν•˜λ©΄ μ—†λŠ” μžλ¦¬λŠ” ν‘œν˜„ν•˜μ§€ μ•Šμ§€λ§Œ, 0은 μ—†λŠ” μžλ¦¬λ„ ν‘œν˜„ν•œλ‹€λŠ” 차이가 μ‘΄μž¬ν•œλ‹€.
λ˜ν•œ, L은 ν•΄λ‹Ή μ§€μ—­μ˜ 톡화 λ‹¨μœ„λ₯Ό λ§ν•˜λŠ” ν˜•μ‹ λ¬Έμžμ΄λ‹€.

πŸ’‘ TO_CHAR ν˜•μ‹ 문자 (숫자)

Formatμ˜ˆμ‹œμ„€λͺ…
, (comma)9,999콀마 ν˜•μ‹μœΌλ‘œ λ³€ν™˜
. (period)99.99μ†Œμˆ˜μ  ν˜•μ‹μœΌλ‘œ λ³€ν™˜
999999ν•΄λ‹Ήμžλ¦¬μ˜ 숫자λ₯Ό μ˜λ―Έν•¨. 값이 없을 경우 μ†Œμˆ˜μ μ΄μƒμ€ 곡백, μ†Œμˆ˜μ μ΄ν•˜λŠ” 0으둜 ν‘œμ‹œ.
009999ν•΄λ‹Ήμžλ¦¬μ˜ 숫자λ₯Ό μ˜λ―Έν•¨. 값이 없을 경우 0으둜 ν‘œμ‹œ. 숫자의 길이λ₯Ό κ³ μ •μ μœΌλ‘œ ν‘œμ‹œν•  경우.
$$9999$ ν†΅ν™”λ‘œ ν‘œμ‹œ
LL9999Local ν†΅ν™”λ‘œ ν‘œμ‹œ(ν•œκ΅­μ˜ 경우 \)
XXXXXXXX16μ§„μˆ˜λ‘œ ν‘œμ‹œ
FMFM1234.56포맷9λ‘œλΆ€ν„° μΉ˜ν™˜λœ 곡백(μ•ž) 및 μ†Œμˆ˜μ μ΄ν•˜0을 제거

TO_DATE

-- 20100101을 YY/MM/DD의 ν˜•μ‹μœΌλ‘œ 바꿔라

SELECT TO_DATE('20100101', 'YY/MM/DD') "TO_DATE" FROM DUAL;

TO_DATEλŠ” 숫자 ν˜Ήμ€ λ¬Έμžν˜• 데이터λ₯Ό λ‚ μ§œν˜• λ°μ΄ν„°λ‘œ λ³€ν™˜ν•˜μ—¬ 리턴 ν•œλ‹€.

-- 직원λͺ…, λΆ€μ„œμ½”λ“œ, 생년월일, λ‚˜μ΄(만) 쑰회
-- 단, 생년월일은 μ£Όλ―Όλ²ˆν˜Έμ—μ„œ μΆ”μΆœν•΄μ„œ, γ…‡γ…‡γ…‡γ…‡λ…„ γ…‡γ…‡μ›” γ…‡γ…‡μΌλ‘œ 좜λ ₯
-- λ‚˜μ΄λŠ” μ£Όλ―Όλ²ˆν˜Έμ—μ„œ μΆ”μΆœν•΄μ„œ λ‚ μ§œλ°μ΄ν„°λ‘œ λ³€ν™˜ν•œ ν›„ 계산

SELECT EMP_NAME "직원λͺ…"
     , DEPT_CODE "λΆ€μ„œμ½”λ“œ"
     , '19' || SUBSTR(EMP_NO, 1, 2) || 'λ…„ ' || SUBSTR(EMP_NO, 3, 2) || 'μ›” ' || SUBSTR(EMP_NO, 5, 2) || '일' "생년월일"
     , EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM TO_DATE(SUBSTR(EMP_NO, 1, 2), 'RR')) "λ‚˜μ΄(만)"
FROM EMPLOYEE;

TO_DATE의 'YY'λŠ” 2000년을 κΈ°μ€€μœΌλ‘œ ν‘œμ‹œν•˜μ§€λ§Œ, 'RR'λŠ” 50 이상은 1900λ…„, 50 λ―Έλ§Œμ€ 2000년을 κΈ°μ€€μœΌλ‘œ λ³€ν™˜ν•˜μ—¬ ν‘œμ‹œν•œλ‹€.

πŸ’‘ TO_DATE ν˜•μ‹

ν˜•μ‹μ˜λ―Έ
YYYY년도 ν‘œν˜„ (4자리)
YY년도 ν‘œν˜„ (2자리)
RR년도 ν‘œν˜„ (2자리),
50이상 1900, 50 미만 2000
MONTH월을 LOCALE 섀정에 맞게 좜λ ₯(FULL)
MM월을 숫자둜 ν‘œν˜„
MON월을 μ•ŒνŒŒλ²³μœΌλ‘œ ν‘œν˜„ (μ›”μš”μΌμ•„λ‹˜)
DDD365일 ν‘œν˜„
DDλ‚ μ§œ ν‘œν˜„
Dμš”μΌμ„ 숫자둜 ν‘œν˜„ (1:μΌμš”μΌ...)
DAYμš”μΌ ν‘œν˜„
DYμš”μΌμ„ μ•½μ–΄λ‘œ ν‘œν˜„
HH HH12μ‹œκ°
HHμ‹œκ° (24μ‹œκ°„)
MIλΆ„
SS초
AM PM A.M. P.M.μ˜€μ „ μ˜€ν›„ ν‘œκΈ°
FMμ›”, 일, μ‹œ, λΆ„, 초 μ•žμ˜ 0을 μ œκ±°ν•¨.

TO_NUMBER

SELECT TO_NUMBER('1,000,000', '9,999,999') "TO_NUMBER" FROM DUAL;

TO_NUMBERλŠ” λ‚ μ§œ ν˜Ήμ€ λ¬Έμžν˜• 데이터λ₯Ό μˆ«μžν˜• λ°μ΄ν„°λ‘œ λ³€ν™˜ν•˜μ—¬ 리턴 ν•œλ‹€.

SELECT '1,000,000' - '500,000' FROM DUAL; -- μž‘λ™ x

SELECT TO_NUMBER('1,000,000', '9,999,999') - TO_NUMBER('500,000', '999,999') "계산" FROM DUAL;

0개의 λŒ“κΈ€