MySQL - MICROSECOND() Function



The MySQL MICROSECOND() function is used to retrieve and return the microseconds from the given date time or time expression.

A micro-second in the date argument can be found as the fractional part of the seconds value within the timestamp. The range of this return value will be from 0 to 999999.

When a NULL value is passed as an argument to this function, the return value will also be returned NULL as there is no timestamp to retrieve the microseconds from.

Syntax

Following is the syntax of MySQL MICROSECOND() function −

MICROSECOND(time);

Parameters

This method accepts the time or datetime expression from which to extract the microsecond part as a parameter.

Return value

This function returns an integer representing the microsecond part of the given time or datetime expression. The value ranges from 0 to 999999.

Example

In the following example, we are using the MySQL MICROSECOND() function to extract the microsecond component from the given time value −

SELECT MICROSECOND('00:00:00 09:40:45.2300') As Result;

Output

This will produce the following result −

Result
230000

Example

The below query will return 0 as output because, we didn't mentioned the milliseconds in the given time value −

SELECT MICROSECOND('00 12:38:48') As Result;

Output

Following is the output −

Result
0

Example

We can also pass the date-time expression as an argument to this function −

SELECT MICROSECOND('2015-09-05 22:40:45.4300') As Result;

Following is the output −

Result
430000

Example

In this example, we have created a table named SALES using the following CREATE TABLE query −

CREATE TABLE SALES(
   CUST_NAME varchar(255),
   PRODUCTNAME varchar(255),
   DISPATCHTIMESTAMP varchar(255),
   LOCATION varchar(255)
);

Now, insert the following records into the SALES table using the INSERT statement −

INSERT INTO SALES VALUES
('Aadhya', 'Key-Board', '2019-05-04 15:02:45.6542', 'Kerala'),
('Varun', 'Earphones', '2019-06-26 14:13:12.6321', 'Mumbai'),
('Vratha', 'Mouse', '2019-12-07 07:50:37.26112','Vijayawada'),
('Rahul', 'Mobile', '2018-03-21 16:00:45.261123', 'Chennai'),
('Maaya', 'Headset', '2018-12-30 10:49:27.21223', 'Goa');

Execute the below query to fetch all the inserted records in the above-created table −

Select * From SALES;

Following is the ORDERS table −

CUST_NAME PRODUCTNAME DISPATCHTIMESTAMP LOCATION
Aadhya Key-Board 2019-05-04 15:02:45.6542 Kerala
Varun Earphones 2019-06-26 14:13:12.6321 Mumbai
Vratha Mouse 2019-12-07 07:50:37.26112 Vijayawada
Rahul Mobile 2018-03-21 16:00:45.261123 Chennai
Maaya Headset 2018-12-30 10:49:27.21223 Goa

Now, we are using the MySQL MICROSECOND() function to retrieve the microsecond values from the "DispatchTimeStamp" column −

SELECT CUST_NAME, DISPATCHTIMESTAMP, MICROSECOND(DispatchTimeStamp)
As Microseconds FROM SALES;

Output

The output is displayed as follows −

CUST_NAME DISPATCHTIMESTAMP Microseconds
Aadhya 2019-05-04 15:02:45.6542 654200
Varun 2019-06-26 14:13:12.6321 632100
Vratha 2019-12-07 07:50:37.26112 261120
Rahul 2018-03-21 16:00:45.261123 261123
Maaya 2018-12-30 10:49:27.21223 212230
Advertisements