How to use the MySQL MICROSECOND() function

MySQL MICROSECOND() is a function that returns the microsecond part of a date or time value.

Posted on

MySQL MICROSECOND() is a function that returns the microsecond part of a date or time value. It can be used to extract the microsecond component from a datetime, date, or time expression. The function takes one argument: the date or time value. The return value is an integer from 0 to 999999, representing the microsecond in the format ‘HH:MM:SS.ffffff’ or HHMMSSffffff.

Syntax

The syntax of the function is:

MICROSECOND(date_or_time)

The parameter is:

  • date_or_time: The date or time value to extract the microsecond from. It can be a datetime, date, or time expression, or a string in a valid date or time format.

Examples

Some examples of using the function are:

  • To get the microsecond part of the current time, use:

    SELECT MICROSECOND(CURTIME());
    

    The result is:

    21
    

    This means that the current time is 10:20:21.000021.

  • To get the microsecond part of a datetime value, use:

    SELECT MICROSECOND('2023-11-15 16:45:12.123456');
    

    The result is:

    456
    

    This means that the datetime value has 456 microseconds.

  • To get the microsecond part of a time value, use:

    SELECT MICROSECOND('18:30:00.987654');
    

    The result is:

    654
    

    This means that the time value has 654 microseconds.

  • To get the microsecond part of a string in a valid date or time format, use:

    SELECT MICROSECOND('11/15/2023 4:45:12.345678 PM');
    

    The result is:

    678
    

    This means that the string is converted to a datetime value and has 678 microseconds.

  • To format a datetime value using the microsecond part, use:

    SELECT DATE_FORMAT('2023-11-15 16:45:12.123456', '%H:%i:%s.%f');
    

    The result is:

    16:45:12.123456
    

    This means that the datetime value is formatted as hour:minute:second.microsecond, using the 24-hour clock.

Similar Functions

Some similar functions to MICROSECOND() are:

  • SECOND(): This function returns the second part of a date or time value, from 0 to 59.
  • MINUTE(): This function returns the minute part of a date or time value, from 0 to 59.
  • HOUR(): This function returns the hour part of a date or time value, from 0 to 23.
  • EXTRACT(): This function extracts a specific part of a date or time value, such as year, month, day, hour, minute, second, or microsecond.