The MySQL MICROSECOND()
function is used to return the microsecond component from a time value.
The return value for this function is in the range 0 to 999999. Below are examples to demonstrate.
Syntax
The syntax of this function goes like this:
MICROSECOND(time)
Where time
is the time value that you want to extract the microseconds component from.
Example 1 – Basic Usage
Here’s an example to demonstrate.
SELECT MICROSECOND('09:40:00.123456');
Result:
+--------------------------------+ | MICROSECOND('09:40:00.123456') | +--------------------------------+ | 123456 | +--------------------------------+
Example 2 – Datetime Values
Here’s an example using an abbreviated datetime value.
SELECT MICROSECOND('2021-10-07 09:40:00.123456');
Result:
+-------------------------------------------+ | MICROSECOND('2021-10-07 09:40:00.123456') | +-------------------------------------------+ | 123456 | +-------------------------------------------+
Example 3 – Smaller Microseconds Value
And here’s an example where the last two digits are the only nonzero digits.
SELECT MICROSECOND('09:40:00.000056');
Result:
+--------------------------------+ | MICROSECOND('09:40:00.000056') | +--------------------------------+ | 56 | +--------------------------------+
Example 4 – No Microseconds Value
And here’s an example where the time argument doesn’t explicitly include a microseconds component.
SELECT MICROSECOND('09:40:00');
Result:
+-------------------------+ | MICROSECOND('09:40:00') | +-------------------------+ | 0 | +-------------------------+
Example 5 – An Alternative
You can also use the EXTRACT()
function to extract the microseconds (and other date/time parts) from a date/time value:
SELECT EXTRACT(MICROSECOND FROM '09:40:00.123456');
Result:
+---------------------------------------------+ | EXTRACT(MICROSECOND FROM '09:40:00.123456') | +---------------------------------------------+ | 123456 | +---------------------------------------------+