작성일자 : 2023-12-21
Ver 0.1.1
1. Intro
MySQL은 관계형 데이터베이스 관리 시스템(RDBMS)으로, 오픈 소스이며 많은 웹 애플리케이션에서 데이터를 저장하고 관리하는 데 있어서 Oracle과 함께 널리 사용되는 시스템 중 하나이다.
MySQL은 Oracle Corporation이 개발한 오픈 소스 RDBMS로, MySQL AB(향후 Sun Microsystems 및 이후 Oracle Corporation으로 인수)에서 초기 개발되었으며, Oracle은 상용 데이터베이스 시스템으로, Oracle Corporation에서 개발 및 유지보수하고 있다.
모두 Oracle Corporation이 개발했고, RDBMS이지만 함수의 사용법은 다소 다르다. 대표적인 것 중 하나가 Date 형식을 변경하는 방법이다.
Oracle은 TO_DATE() 함수를 사용하지만, MySQL에서는 DATE_FORMAT 함수를 사용한다.
[SQL]DATE
작성일자 : 2023-07-04 수정일자 : 2023-07-23 Ver 0.1.2 1. 날짜 쿼리문을 작성함에 있어서 날짜를 잘 다루는 것은 기본이자 필수이다. 나 같은 경우는 실제로 프로젝트 당시 태블로로 대시보드를 만든 후
junius96.com
2. DATE_FORMAT()
기본 사용법은 아래와 같다.
SELECT DATE_FORMAT(date, format)
FROM TABLE_NM
format에 들어갈수 있는 형태는 아래 표와 같으며, 해당 표는 MySQL 공식 참조 문서의 내용이다.
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) |
%b | Abbreviated month name (Jan..Dec) |
%c | Month, numeric (0..12) |
%D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, …) |
%d | Day of the month, numeric (00..31) |
%e | Day of the month, numeric (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) |
%l | Hour (1..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) |
%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 |
%v | Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %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 |
%x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
%Y | Year, numeric, four digits |
%y | Year, numeric (two digits) |
%% | A literal % character |
%x | x, for any “x” not listed above |
위 표를 활용하여 가장 일반적인 '년월일시분초'에 대해서 표현해보았다.
SELECT DATE_FORMAT(SYSDATE(), '%y년 %m월 %d일 %H시 %i분 %s초') AS SYSDATE
FROM DUAL -- 생략가능;
위 표를 잘 활용하여 상황에 맞는 쿼리문을 작성 하면 될 것 같다.