Loading

MySQL Date Or Time Function Part-7

In MySQL Date Or Time Function Part-7 we will discuss about the SYSDATE, TIME, TIMEDIFF, TIMESTAMP, TIME_FORMAT, TIME_TO_SEC

SYSDATE Function:- SYSDATE Function is used to return the current server date and time. The format of the date and time will be “YYYY-MM-DD HH:MM: SS” (string) or as YYYYMMDDHHMMSS (numeric).

Syntax:- SYSDATE()

Example:-

SELECT SYSDATE();

Output:- 2023-02-14 10:16:56

TIME Function:- TIME Function is used for extracting the time part from a given date or date and time expression.

Syntax:- TIME(expression)

Example:-

SELECT TIME(“2023-02-14 13:50:30”);

Output:- 13:50:30

TIME_FORMAT Function:- TIME_FORMAT Function is used for formatting the time in a specific format from a given date and time.

Syntax:- TIME_FORMAT(time, format)

The following format will be used either single or combination:-

%f For   Microseconds (000000 to 999999)

%H For  Hour (00 to 23)

%h For  Hour (00 to 12)

%I For   Hour (00 to 12)

%i For    Minutes (00 to 59)

%p For  AM or PM

%r For   Time in 12 hour AM or PM format (hh:mm:ss AM/PM)

%S For  Seconds (00 to 59)

%s For   Seconds (00 to 59)

%T For  Time in 24 hour format (hh:mm:ss)

Example:- SELECT TIME_FORMAT(“15:57:40”, “%h %i %s %p”);

Output:- 03 57 40 PM

TIME_TO_SEC Function:- TIME_TO_SEC Function is used for converting time value into seconds.

Syntax:- TIME_TO_SEC(time value)

Example:-

SELECT TIME_TO_SEC(“14:41:37”);

Output:- 52897

TIMEDIFF Function:- TIMEDIFF Function is used for the difference between the Same format at two different times.

Syntax:- TIMEDIFF(time1, time2)

Example:-

SELECT TIMEDIFF(“14:44:34”, “12:50:19”);

Output:-

01:54:15

TIMESTAMP Function:- TIMESTAMP Function is used for returning the date-time value from a given date-time value. For a single argument, it returns the date or datetime expression and in the case of two arguments, It returns the sum of two argument date or datetime expression.

Syntax:- TIMESTAMP(expression) or TIMESTAMP(expression1, expression2)

Example:-

SELECT TIMESTAMP(“2023-02-15”,  “14:53:42”);

Output:- 2023-02-15 14:53:42

SELECT TIMESTAMP(“2023-02-15”);

Output:- 2023-02-15 00:00:00

Read Also:-

MySQL Date Or Time Function Part-1
MySQL Date Or Time Function Part-2
MySQL Date Or Time Function Part-3
MySQL Date Or Time Function Part-4
MySQL Date Or Time Function Part-5
MySQL Date Or Time Function Part-6
MySQL Date Or Time Function Part-8

Read more detail about MySQL Date Or Time Function manual

Share with:


Leave a Reply

Connect with:

z35W7z4v9z8w
Verified by ExactMetrics