HEROJOON 블로그(히로블)

Mysql Datetime Function 더하기, 빼기 본문

DB

Mysql Datetime Function 더하기, 빼기

herojoon 2021. 2. 2. 00:00
반응형

목표

Mysql Function을 이용하여 Datetime의 시간을 더하고 빼줍니다.

 

DATE_ADD -> 더하기

DATE_SUB -> 빼기

 

#### 날짜 & 시간 더하기 ####

# 날짜에 DAY (일) 더하기
SELECT DATE_ADD('2021-02-01', INTERVAL 15 DAY);
# => 2021-02-16

# 날짜에 WEEK (주) 더하기
SELECT DATE_ADD('2021-02-01', INTERVAL 1 WEEK);
# => 2021-02-08

# 날짜에 MONTH (달) 더하기
SELECT DATE_ADD('2021-02-01', INTERVAL 1 MONTH);
# => 2021-03-01

# 날짜에 YEAR (년) 더하기
SELECT DATE_ADD('2021-02-01', INTERVAL 1 YEAR);
# => 2022-02-01

# 날짜에 SECOND (초) 더하기
SELECT DATE_ADD('2021-02-01 00:00:00', INTERVAL '30' SECOND);
# => 2021-02-01 00:00:30

# 날짜에 MINUTE, SECOND (분, 초) 더하기
SELECT DATE_ADD('2021-02-01 00:00:00', INTERVAL '20:30' MINUTE_SECOND);
# => 2021-02-01 00:20:30

# 날짜에 HOUR, MINUTE, SECOND (시, 분, 초) 더하기
SELECT DATE_ADD('2021-02-01 00:00:00', INTERVAL '10:20:30' HOUR_SECOND);
# => 2021-02-01 10:20:30

# 날짜에 DAY, SECOND (일, 초) 더하기
SELECT DATE_ADD('2021-02-01 00:00:00', INTERVAL '1 00:00:30' DAY_SECOND);
# => 2021-02-02 00:00:30

# 날짜에 DAY, MINUTE (일, 분) 더하기
SELECT DATE_ADD('2021-02-01 00:00:00', INTERVAL '1 00:20' DAY_MINUTE);
# => 2021-02-02 00:20:00

# 날짜에 DAY, HOUR (일, 시) 더하기
SELECT DATE_ADD('2021-02-01 00:00:00', INTERVAL '1 10' DAY_HOUR);
# => 2021-02-02 10:00:00

# 날짜에 DAY, HOUR, MINUTE, SECOND (일, 시, 분, 초) 더하기
SELECT DATE_ADD('2021-02-01 00:00:00', INTERVAL '1 10:20:30' DAY_SECOND);
# => 2021-02-02 10:20:30



#### 날짜 & 시간 빼기 ####

# 날짜에 DAY (일) 빼기
SELECT DATE_SUB('2021-02-20', INTERVAL 15 DAY);
# => 2021-02-05

# 날짜에 WEEK (주) 빼기
SELECT DATE_SUB('2021-02-20', INTERVAL 1 WEEK);
# => 2021-02-13

# 날짜에 MONTH (달) 빼기
SELECT DATE_SUB('2021-02-20', INTERVAL 1 MONTH);
# => 2021-01-20

# 날짜에 YEAR (년) 빼기
SELECT DATE_SUB('2021-02-20', INTERVAL 1 YEAR);
# => 2020-02-20

# 날짜에 SECOND (초) 빼기
SELECT DATE_SUB('2021-02-01 23:59:59', INTERVAL '30' SECOND);
# => 2021-02-01 23:59:29

# 날짜에 MINUTE, SECOND (분, 초) 빼기
SELECT DATE_SUB('2021-02-01 23:59:59', INTERVAL '20:30' MINUTE_SECOND);
# => 2021-02-01 23:39:29

# 날짜에 HOUR, MINUTE, SECOND (시, 분, 초) 빼기
SELECT DATE_SUB('2021-02-01 23:59:59', INTERVAL '10:20:30' HOUR_SECOND);
# => 2021-02-01 13:39:29

# 날짜에 DAY, SECOND (일, 초) 빼기
SELECT DATE_SUB('2021-02-01 23:59:59', INTERVAL '1 00:00:30' DAY_SECOND);
# => 2021-01-31 23:59:29

# 날짜에 DAY, HOUR (일, 분) 빼기
SELECT DATE_SUB('2021-02-01 23:59:59', INTERVAL '1 00:20' DAY_MINUTE);
# => 2021-01-31 23:39:59

# 날짜에 DAY, HOUR (일, 시) 빼기
SELECT DATE_SUB('2021-02-01 23:59:59', INTERVAL '1 10' DAY_HOUR);
# => 2021-01-31 13:59:59

# 날짜에 DAY, HOUR, MINUTE, SECOND (일, 시, 분, 초) 빼기
SELECT DATE_SUB('2021-02-01 00:00:00', INTERVAL '1 10:20:30' DAY_SECOND);
# => 2021-01-30 13:39:30

 

 

참고링크

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

 

반응형
Comments