๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Database·SQL

Oracle์˜ TO_CHAR ํ•จ์ˆ˜์™€ ๋Œ€์‘๋˜๋Š” MySQL ํ•จ์ˆ˜

by Leica 2020. 12. 6.
๋ฐ˜์‘ํ˜•

Oracle TO_CHAR -> MySQL DATE_FORMAT

Oracle์˜ TO_CHAR๋Š” MySQL์˜ DATE_FORMAT๊ณผ ๋Œ€์‘๋œ๋‹ค.

 

โ„น๏ธ Oracle TO_CHAR ์‚ฌ์šฉ ์˜ˆ

select TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') from dual
20201206153514

 

โ„น๏ธ MySQL DATE_FORMAT ์‚ฌ์šฉ ์˜ˆ

select DATE_FORMAT(now(), '%Y%m%d%H%i%s')
20201206154329

 

Oracle TO_CHAR

Syntax:

TO_CHAR(์ˆซ์ž/๋‚ ์งœ, ํฌ๋งท)
  • ์ˆซ์ž๋‚˜ ๋‚ ์งœ๋ฅผ ํŠน์ • ํฌ๋งท์˜ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ˆซ์ž/๋‚ ์งœ๋ฅผ, ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํฌ๋งท์„ ์ง€์ •ํ•œ๋‹ค.

 

TO_CHAR ๋‚ ์งœ ๋ณ€ํ™˜ ํฌ๋งท

ํฌ๋งท ์„ค๋ช… ์‚ฌ์šฉ ์˜ˆ
AM, A.M. ์˜ค์ „ TO_CHAR(SYSDATE, ‘AM’) → ์˜ค์ „
PM, P.M. ์˜คํ›„ TO_CHAR(SYSDATE, ‘PM’) → ์˜คํ›„
YYYY, YYY, YY, Y ์—ฐ๋„ TO_CHAR(SYSDATE, ‘YYYY’) → 2014
MONTH, MON ์›” TO_CHAR(SYSDATE, ‘MONTH’) → 2์›”
MM 01~12 ํ˜•ํƒœ์˜ ์›” TO_CHAR(SYSDATE, ‘MM’) → 02
D ์ฃผ์ค‘์˜ ์ผ์„ 1~7๋กœ TO_CHAR(SYSDATE, ‘D’) → 2
DAY ์ฃผ์ค‘ ์ผ์„ ์š”์ผ๋กœ ํ‘œ์‹œ TO_CHAR(SYSDATE, ‘DAY’) → ์›”์š”์ผ
DD ์ผ์„ 01~31 ํ˜•ํƒœ๋กœ ํ‘œ์‹œ TO_CHAR(SYSDATE, ‘DD’) → 01
DDD ์ผ์„ 001~365 ํ˜•ํƒœ๋กœ TO_CHAR(SYSDATE, ‘DDD’) → 041
DL ํ˜„์žฌ ์ผ์„ ์š”์ผ๊นŒ์ง€ ํ‘œ์‹œ TO_CHAR(SYSDATE, ‘DL’) → 2014๋…„ 2์›” 10์ผ ์›”์š”์ผ
HH, HH12 ์‹œ๊ฐ„์„ 01~12์‹œ ํ˜•ํƒœ๋กœ TO_CHAR(SYSDATE, ‘HH’) → 04
HH24 ์‹œ๊ฐ„์„ 01~23์‹œ ํ˜•ํƒœ๋กœ TO_CHAR(SYSDATE, ‘HH24’) → 16
MI ๋ถ„์„ 00~59๋ถ„ ํ˜•ํƒœ๋กœ TO_CHAR(SYSDATE, ‘MI’) → 56
SS ์ดˆ๋ฅผ 01~59์ดˆ ํ˜•ํƒœ๋กœ TO_CHAR(SYSDATE, ‘SS’) → 33
FF ๋ฐ€๋ฆฌ์ดˆ(milliseconds) TO_CHAR(SYSTIMESTAMP, 'YYYYMMDDHH24MISSFF')
WW ์ฃผ๋ฅผ 01~53์ฃผ ํ˜•ํƒœ๋กœ TO_CHAR(SYSDATE, ‘WW’) → 06

 

TO_CHAR ์ˆซ์ž ๋ณ€ํ™˜ ํฌ๋งท

ํฌ๋งท ์„ค๋ช… ์‚ฌ์šฉ ์˜ˆ
, (์ฝค๋งˆ) ์ฝค๋งˆ๋กœ ํ‘œ์‹œ TO_CHAR(123456, ‘999,999’) → 123,456
. (์†Œ์ˆ˜์ ) ์†Œ์ˆ˜์  ํ‘œ์‹œ TO_CHAR(123456.4, ‘999,999.9’) → 123,456.4
9 ํ•œ ์ž๋ฆฌ ์ˆซ์ž, ์‹ค์ œ ๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ฒŒ ๋ช…์‹œ TO_CHAR(123456, ‘999,999’) → 123,456
PR ์Œ์ˆ˜์ผ ๋•Œ < >๋กœ ํ‘œ์‹œ TO_CHAR(-123, ‘999PR’) → <123>
RN, rn ๋กœ๋งˆ ์ˆซ์ž๋กœ ํ‘œ์‹œ TO_CHAR(123, ‘RN’)→CXXIII
S ์–‘์ˆ˜์ด๋ฉด +, ์Œ์ˆ˜์ด๋ฉด - ํ‘œ์‹œ TO_CHAR(123, ‘S999’) → +123

 

๋ฐ˜์‘ํ˜•

 

MySQL DATE_FORMAT

Syntax:

DATE_FORMAT(๋‚ ์งœ, ํฌ๋งท)
  • ๋‚ ์งœ๋ฅผ ํŠน์ • ํฌ๋งท์˜ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋‚ ์งœ๋ฅผ, ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํฌ๋งท์„ ์ง€์ •ํ•œ๋‹ค.

 

DATE_FORMAT ํฌ๋งท ์ •๋ฆฌ

ํฌ๋งท ์„ค๋ช…
%a ์ผ(Sun ~ Sat)
%b ์›”(Jan ~ Dec)
%c ์›”(0 ~ 12)
%D ์ผ(1st, 2nd, 3rd, ...)
%d ์ผ(01 ~ 31)
%e ์ผ(0 ~ 31)
%f Microseconds (000000 to 999999)
%H ์‹œ๊ฐ„(00 ~ 23)
%h ์‹œ๊ฐ„(00 ~ 12)
%I ์‹œ๊ฐ„(00 ~ 12)
%i ๋ถ„(00 ~ 59)
%j Day of the year (001 to 366)
%k ์‹œ๊ฐ„(0 ~ 23)
%l ์‹œ๊ฐ„(1 ~ 12)
%M ์›”(January ~ December)
%m ์›”(00 ~ 12)
%p AM or PM
%r ์‹œ๊ฐ„(12์‹œ)์„ hh:mm:ss AM/PM ํ˜•์‹์œผ๋กœ
%S ์ดˆ(00 ~ 59)
%s ์ดˆ(00 ~ 59)
%T ์‹œ๊ฐ„(24์‹œ)์„ hh:mm:ss ํ˜•์‹์œผ๋กœ
%U Week where Sunday is the first day of the week (00 ~ 53)
%u Week where Monday is the first day of the week (00 ~ 53)
%V Week where Sunday is the first day of the week (01 ~ 53). Used with %X
%v Week where Monday is the first day of the week (01 ~ 53). Used with %x
%W ์ผ(Sunday ~ Saturday)
%w Day of the week where Sunday=0 and Saturday=6
%X Year for the week where Sunday is the first day of the week. Used with %V
%x Year for the week where Monday is the first day of the week. Used with %v
%Y ์—ฐ๋„(4์ž๋ฆฌ)
%y ์—ฐ๋„(2์ž๋ฆฌ)

 

TO_CHAR & DATE_FORMAT ํฌ๋งท ๋งคํ•‘

Oracle์˜ TO_CHAR ํ•จ์ˆ˜๋ฅผ MySQL์˜ DATE_FORMAT์œผ๋กœ ๋ณ€๊ฒฝํ•  ๋•Œ ๋Œ€์‘๋˜๋Š” ํฌ๋งท์„ ํ‘œ๋กœ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค.

๊ฒฐ๊ณผ Oracle TO_CHAR MySQL DATE_FORMAT
์—ฐ๋„(4์ž๋ฆฌ) YYYY %Y
์—ฐ๋„(2์ž๋ฆฌ) YY %y
์—ฐ๋„(4์ž๋ฆฌ, YYYY์™€ ๋™์ผ) RRRR %Y
์—ฐ๋„(2์ž๋ฆฌ, YY์™€ ๋™์ผ) RR %y
์›”(1~12) MM %m
์›”(Jan~Dec) MON %b
์›”(January~December) MONTH %M
์ผ(1~31) DD %d
์ผ(Sun~Sat) DY %a
์‹œ๊ฐ„(0~23) HH24 %H
์‹œ๊ฐ„(1~12) HH ๋˜๋Š” HH12 %h
๋ถ„(0~59) MI %i
์ดˆ(0~59) SS %s

โ„น๏ธ Oracle์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํฌ๋งท 'YYYYMMDDHH24MISS'์„ MySQL๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด '%Y%m%d%H%i%s'์ด๋‹ค.

โ„น๏ธ YYYY์™€ RRRR์€ ๋™์ผํ•˜๋‹ค๊ณ  ๋ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

โ„น๏ธ YY์™€ RR์€ TO_CHAR์—์„œ๋Š” ๋™์ผํ•˜๊ณ  TO_DATE์—์„œ๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

 

โ„น๏ธ Oracle YY์™€ RR ํฌ๋งท์˜ ์ฐจ์ด์ :

RRRR๊ณผ YYYY๋Š” 4์ž๋ฆฌ ์—ฐ๋„์ด๋ฏ€๋กœ ์ฐจ์ด๊ฐ€ ์—†๋‹ค๊ณ  ๋ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

๋‹จ, 2์ž๋ฆฌ ํ‘œ์‹œํ˜•์‹์ธ RR๊ณผ YY๋Š” ์—ฐ๋„ ์•ž ๋‘์ž๋ฆฌ๊ฐ€ ์ƒ๋žต๋˜์–ด ๊ทธ ์˜๋ฏธ๊ฐ€ ๋‹ค๋ฅด๋‹ค.
R์€ ROUND ์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

100๋…„ ๋‹จ์œ„๋กœ ์—ฐ๋„๋ฅผ ๋ฐ˜์˜ฌ๋ฆผํ•œ ๊ฒƒ์ด ๊ธฐ์ค€๋…„๋„๊ฐ€ ๋˜๊ณ  ์ž…๋ ฅ๋˜๋Š” ๋‘ ์ž๋ฆฌ ์—ฐ๋„๊ฐ’์ด 50 ๋ฏธ๋งŒ์ด๋ฉด ๊ธฐ์ค€๋…„๋„์˜ ์•ž ๋‘ ์ž๋ฆฌ๋ฅผ, 50 ์ด์ƒ์ด๋ฉด ๊ธฐ์ค€๋…„๋„ ์ „๋…„๋„์˜ ์•ž ๋‘์ž๋ฆฌ๋ฅผ ๋ถ™์ธ๋‹ค.

์˜ˆ๋ฅผ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ํ›จ์”ฌ ์‰ฝ๋‹ค.

ํ˜„์žฌ๋…„๋„๊ฐ€ 2020๋…„์ด๋ฉด ๋ฐ˜์˜ฌ๋ฆผํ•œ 2000๋…„์ด ๊ธฐ์ค€๋…„๋„๊ฐ€ ๋˜๊ณ , ๊ธฐ์ค€๋…„๋„ ์ „๋…„๋„๋Š” 1999๋…„์ด๋‹ค.
 - TO_DATE('140101', 'rrmmdd') ==> 14๋Š” 50 ๋ฏธ๋งŒ์ด๋ฏ€๋กœ ==> 2014-01-01
 - TO_DATE('940101', 'rrmmdd') ==> 94๋Š” 50 ์ด์ƒ์ด๋ฏ€๋กœ ==> 1994-01-01

ํ˜„์žฌ๋…„๋„๊ฐ€ 2054๋…„์ด๋ฉด ๋ฐ˜์˜ฌ๋ฆผํ•œ 2100๋…„์ด ๊ธฐ์ค€๋…„๋„๊ฐ€ ๋˜๊ณ , ๊ธฐ์ค€๋…„๋„ ์ „๋…„๋„๋Š” 2099๋…„์ด๋‹ค.
 - TO_DATE('140101', 'rrmmdd') ==> 14๋Š” 50 ๋ฏธ๋งŒ์ด๋ฏ€๋กœ ==> 2114-01-01
 - TO_DATE('940101', 'rrmmdd') ==> 94๋Š” 50 ์ด์ƒ์ด๋ฏ€๋กœ ==> 2094-01-01

 

References

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€