DATE_FORMAT()
MySQL에서는 날짜나 시간을 다룰 때 이 DATE_FORMAT()이라는 함수를 사용해서 날짜나 시간을 다룸 !
사용법
사용법은 이렇다
DATE_FORMAT(날짜관련컬럼명, '%Y-%m-%d')
시간이나 날짜와 관련된 컬럼명을 첫번째 인자에 넣어주고, 어떤 형식으로 변환하고 싶은지를 두번째 인자에 넣어주면 된다 !
DATE_FORMAT("날짜값", '%Y-%m-%d')
이렇게 시간이나 날짜 관련 컬럼이 아니라 바로 시간을 넣어줘도 가능 !
사용 가능한 표현식 모음
MySQL 8.0 공식 문서에서 참고한 표현식의 종류임 !!!
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html
MySQL :: MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions
12.7 Date and Time Functions This section describes the functions that can be used to manipulate temporal values. See Section 11.2, “Date and Time Data Types”, for a description of the range of values each date and time type has and the valid formats
dev.mysql.com
형식 | 설명 | 번역 |
%a | Abbreviated weekday name (Sun..Sat) | 요일 명을 Sun, ..., Sat으로 |
%b | Abbreviated month name (Jan..Dec) | 월 이름을 Jan, ..., Dec으로 |
%c | Month, numeric (0..12) | 월 이름을 0, ..., 12으로 |
%D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, …) | 일 이름을 1st, 2nd, 3rd, ...로 |
%d | Day of the month, numeric (00..31) | 일을 00, ..., 31로 |
%e | Day of the month, numeric (0..31) | 일을 0, ..., 31로 |
%f | Microseconds (000000..999999) | 마이크로초 |
%H | Hour (00..23) | ... |
%h | Hour (01..12) | |
%I | Hour (01..12) | |
%i | Minutes, numeric (00..59) | |
%j | Day of year (001..366) | |
%k | Hour (0..23) | 24시간 표기법 |
%l | Hour (1..12) | 12시간 표기법 |
%M | Month name (January..December) | |
%m | Month, numeric (00..12) | |
%p | AM or PM | |
%r | Time, 12-hour (hh:mm:ss followed by AM or PM) | |
%S | Seconds (00..59) | |
%s | Seconds (00..59) | |
%T | Time, 24-hour (hh:mm:ss) | 24시간 표기법 |
%U | Week (00..53), where Sunday is the first day of the week; WEEK() mode 0 | 일년에 몇번째 주인지, 일요일이 일주일의 시작 |
%u | Week (00..53), where Monday is the first day of the week; WEEK() mode 1 | 위와 동일, 월요일이 일주일의 시작 |
%V | Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X | %X와 같이 쓰임 |
%v | Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x | %x와 같이 쓰임 |
%W | Weekday name (Sunday..Saturday) | |
%w | Day of the week (0=Sunday..6=Saturday) | |
%X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V | %V와 같이 쓰임 |
%x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v | %v와 같이 쓰임 |
%Y | Year, numeric, four digits | |
%y | Year, numeric (two digits) | |
%% | A literal % character | |
%x | x, for any “x” not listed above |
위에서 부터 다 해석을 달려고 했는데 다 비슷비슷하고 쉽게 알아먹을 수 있을 것 같아서 해석을 하다가 말았음 ㅎㅎㅎㅎ 잘 보시면 아마 쓰는 것만 쓰고 나머지는 잘 안쓸거에요 ~~~ 필요한 거 있을 때 찾아서 쓰는게 좋을 듯 함 !
결론
'%Y-%m-%d'나 많이 쓰이는 것은 외워두면 편하고, 나머지 특이한 것들은 쓰고 싶을 때 찾아서 쓰는게 낫다 ~~
아 그리고, Oracle에서는 TO_CHAR()를 사용해서 변환한다고 함