Sunday, December 22, 2024

MySQL TIME() Function

 The TIME() function in MySQL is used to extract the time part from a given date or datetime expression. It returns a time value in the format HH:MM:SS.

Syntax:

TIME(expression)
  • expression: This can be a date, datetime, or timestamp value.

Example Usage:

  1. Extracting the time from a datetime value:

    SELECT TIME('2024-12-23 14:30:45');
    

    Result: 14:30:45

  2. Using with columns in a table: If you have a datetime or timestamp column, you can extract just the time part:

    SELECT TIME(order_datetime) FROM orders;
    
  3. Using with the current time: You can also use TIME() to get the current time:

    SELECT TIME(NOW());
    

Notes:

  • The TIME() function returns a value in the format HH:MM:SS.
  • If the input is a string that cannot be converted to a valid time, it returns NULL.

Let me know if you'd like further clarification!

No comments:

Post a Comment