mysql 날짜/시간 함수 정리
-- 현재시간 날짜 구하기
select now(), current_date, current_time
-- 날짜 더하고 빼기
select now(), date_add(now(), interval 1 day)
select now(), date_add(now(), interval 4 hour)
select now(), date_sub(now(), interval 1 day)
select now(), date_sub(now(), interval 4 hour)
-- 두 시간의 차이
select timestampdiff(MONTH, now(), date_add(now(), interval 4 month))
시간주기에 아래의 단위를 사용할 수 있다.
SECOND : 초
MINUTE : 분
HOUR : 시
DAY : 일
WEEK : 주
MONTH : 월
QUARTER : 분기
YEAR : 연
-- date 또는 datetime 데이터 포매팅하기
select now(), date_format(now, '%Y-%m-%d')
-- 문자열을 시간으로 변경하기
select str_to_date('20220728091133', '%Y%m%d%H%i%s')
mysql Format String
%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 |
참고 : https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
'분석 > 데이터분석' 카테고리의 다른 글
[SQL] 주단위 집계 꼭!! 주의해야할 점 (MySql) (0) | 2022.05.16 |
---|---|
Mysql 샘플링 (Sampling) : 랜덤 추출 (0) | 2022.04.06 |
애플뮤직 A/B Test ? (0) | 2021.10.02 |
일련번호(날짜) 생성 in Presto (0) | 2021.04.20 |
M+3 의 재 구매율, M+(1,2,3)의 재 구매율 (6) | 2020.06.25 |