Sunday, December 22, 2024

MySQL MICROSECOND() Function

 The MICROSECOND() function in MySQL is used to extract the microsecond part of a given time or datetime value. It returns the microsecond portion as an integer value, representing the number of microseconds (i.e., millionths of a second) in the specified time.

Syntax:

MICROSECOND(time)
  • time: A TIME, DATETIME, or TIMESTAMP expression from which you want to extract the microseconds.

Example Usage:

  1. Using MICROSECOND() with a DATETIME value:

    SELECT MICROSECOND('2024-12-23 14:30:45.123456');
    

    This will return 123456 as the microsecond part of the given DATETIME.

  2. Using MICROSECOND() with a TIME value:

    SELECT MICROSECOND('14:30:45.123456');
    

    This will return 123456, which is the microsecond part of the given TIME.

  3. Extracting the microsecond from the current timestamp:

    SELECT MICROSECOND(NOW());
    

    This will return the microsecond part of the current timestamp.

Notes:

  • If the input does not contain a microsecond part (for example, if it's just TIME or DATETIME without fractional seconds), MICROSECOND() will return 0.
  • The function operates on the fractional part of a time, which includes the microsecond component if present.

No comments:

Post a Comment