Jump to: navigation, search

Mysql DATE SUB

From w3cyberlearnings

Contents

MySQL DATE_SUB Function

This function removes the date, month, or year from the expression and return.

Syntax DATE_SUB

  • date: is the date string or timestamp
  • INTERVAL: key word
  • expr: valid number
  • unit: DAY, MONTH, YEAR
DATE_SUB(date, INTERVAL expr unit);

Format

unit Value	Expected expr Format
----------------------------------------
MICROSECOND	MICROSECONDS
SECOND	        SECONDS
MINUTE	        MINUTES
HOUR	        HOURS
DAY	        DAYS
WEEK	        WEEKS
MONTH	        MONTHS
QUARTER	        QUARTERS
YEAR	        YEARS
SECOND_MICROSECOND	'SECONDS.MICROSECONDS'
MINUTE_MICROSECOND	'MINUTES:SECONDS.MICROSECONDS'
MINUTE_SECOND	        'MINUTES:SECONDS'
HOUR_MICROSECOND	'HOURS:MINUTES:SECONDS.MICROSECONDS'
HOUR_SECOND	'HOURS:MINUTES:SECONDS'
HOUR_MINUTE	'HOURS:MINUTES'
DAY_MICROSECOND	'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
DAY_SECOND	'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE	'DAYS HOURS:MINUTES'
DAY_HOUR	'DAYS HOURS'
YEAR_MONTH	'YEARS-MONTHS'

Example 1: remove 3 days

mysql> SELECT DATE_SUB('2012-07-21', INTERVAL 3 DAY);
+----------------------------------------+
| DATE_SUB('2012-07-21', INTERVAL 3 DAY) |
+----------------------------------------+
| 2012-07-18                             |
+----------------------------------------+
1 row in set (0.00 sec)

Example 2:remove 3 months

mysql> SELECT DATE_SUB('2012-07-21', INTERVAL 3 MONTH);
+------------------------------------------+
| DATE_SUB('2012-07-21', INTERVAL 3 MONTH) |
+------------------------------------------+
| 2012-04-21                               |
+------------------------------------------+
1 row in set (0.00 sec)


Example 3:remove 3 years

mysql> SELECT DATE_SUB('2012-07-21', INTERVAL 3 YEAR);
+-----------------------------------------+
| DATE_SUB('2012-07-21', INTERVAL 3 YEAR) |
+-----------------------------------------+
| 2009-07-21                              |
+-----------------------------------------+
1 row in set (0.00 sec)


Related Links



Navigation
Web
SQL
MISC
References