MySQL SUBTIME Function

Example

Subtract 5.000001 seconds and return the datetime:

SELECT SUBTIME("2017-06-15 10:24:21.000004", "5.000001");

Definition and Usage

The SUBTIME() function subtracts time from a time/datetime expression and then returns the new time/datetime.

Syntax

SUBTIME(datetime, time_interval)

Continue reading MySQL SUBTIME Function

MySQL SUBDATE Function

Example

Subtract 10 days from a date and return the date:

SELECT SUBDATE("2017-06-15", INTERVAL 10 DAY);

Definition and Usage

The SUBDATE() function subtracts a time/date interval from a date and then returns the date.

Syntax

SUBDATE(date, INTERVAL value unit)

OR:

SUBDATE(date, days)

Continue reading MySQL SUBDATE Function

MySQL STR_TO_DATE Function

Example

Return a date based on a string and a format:

SELECT STR_TO_DATE("August 10 2017", "%M %d %Y");

Definition and Usage

The STR_TO_DATE() function returns a date based on a string and a format.

Syntax

STR_TO_DATE(string, format)

Continue reading MySQL STR_TO_DATE Function

MySQL SEC_TO_TIME Function

Example

Return a time value based on a specified seconds value:

SELECT SEC_TO_TIME(1);

Definition and Usage

The SEC_TO_TIME() function returns a time value (in format HH:MM:SS) based on the specified seconds.

Syntax

SEC_TO_TIME(seconds)

Continue reading MySQL SEC_TO_TIME Function

MySQL SECOND Function

Example

Return the seconds part of a datetime value:

SELECT SECOND("2017-06-20 09:34:00.000023");

Definition and Usage

The SECOND() function returns the seconds part of a time/datetime (from 0 to 59).

Syntax

SECOND(datetime)

Continue reading MySQL SECOND Function

MySQL QUARTER Function

Example

Return the quarter of the year for the date:

SELECT QUARTER("2017-06-15");

Definition and Usage

The QUARTER() function returns the quarter of the year for a given date value (a number from 1 to 4).

  • January-March returns 1
  • April-June returns 2
  • July-Sep returns 3
  • Oct-Dec returns 4

Syntax

QUARTER(date)

Continue reading MySQL QUARTER Function

MySQL PERIOD_DIFF Function

Example

Return the difference between two periods:

SELECT PERIOD_DIFF(201710, 201703);

Definition and Usage

The PERIOD_DIFF() function returns the difference between two periods. The result will be in months.

Note: period1 and period2 should be in the same format.

Syntax

PERIOD_DIFF(period1, period2)

Continue reading MySQL PERIOD_DIFF Function

MySQL PERIOD_ADD Function

Example

Add a specified number of months to a period:

SELECT PERIOD_ADD(201703, 5);

Definition and Usage

The PERIOD_ADD() function adds a specified number of months to a period.

The PERIOD_ADD() function will return the result formatted as YYYYMM.

Syntax

PERIOD_ADD(period, number)

Continue reading MySQL PERIOD_ADD Function

MySQL NOW Function

Example

Return current date and time:

SELECT NOW();

Definition and Usage

The NOW() function returns the current date and time.

Note: The date and time is returned as “YYYY-MM-DD HH-MM-SS” (string) or as YYYYMMDDHHMMSS.uuuuuu (numeric).

Syntax

NOW()

Continue reading MySQL NOW Function

MySQL MONTHNAME Function

Example

Return the name of the month for a date:

SELECT MONTHNAME("2017-06-15");

Definition and Usage

The MONTHNAME() function returns the name of the month for a given date.

Syntax

MONTHNAME(date)

Continue reading MySQL MONTHNAME Function