MICROSECOND
MICROSECOND is a function in SQL that returns the microseconds from a specified time. The integer returned falls in the range from 0 to 999999.
MICROSECOND(expression)
- expression: This parameter is required and specifies the time value for which to return the microsecond part. For example, if the provided time value is ‘2009-05-18 15
Example
SELECT MICROSECOND('2022-03-15 12:35:49.123456');
Output
123456
Explanation
In MySQL, the MICROSECOND()
function extracts the microsecond part from a time or datetime value. In the provided example, MICROSECOND('2022-03-15 12:35:49.123456')
is used to extract microseconds from the provided datetime value, resulting in the output 123456
.
MICROSECOND(timestamp)
- timestamp: The input parameter representing the date and time value from which the microsecond part is to be extracted. It must be of a timestamp data type, string, or an expression that evaluates to a date and time value.
Example
SELECT EXTRACT(MICROSECOND FROM TIMESTAMP '2000-12-16 12:21:13.456789');
Output
456789
Explanation
The EXTRACT()
function retrieves a field, such as MICROSECOND, from a timestamp value. In the given example, it extracts the microsecond portion from the specified timestamp, resulting in 456789
.
MICROSECOND( date_value )
- date_value: The exact point in time from which the microseconds are to be extracted, expressed in a valid date format.
Example
SELECT DATEPART(MICROSECOND, '00:00:01.1234567') AS Microseconds;
Output
Microseconds------------456700
Explanation
The DATEPART function in SQL Server is used to return a part of a date such as year, month, day, hour, minute, second, or microsecond. In the example above, the DATEPART function is used to extract the microsecond portion of the time ‘00:00:01.1234567’. The output shows the microsecond as ‘456700’.
MICROSECOND(EXPRESSION)
- expression: Represents the input timestamp or interval value from which the microsecond part is to be extracted. If a date value is used as the expression, the microsecond part is always 0 because Oracle Database doesn’t store fractional seconds in a DATE value. If it’s a timestamp or interval value, the microsecond part is extracted.
Example
SELECT EXTRACT(MICROSECOND FROM SYSTIMESTAMP) AS MicrosecondsFROM dual;
Output
MICROSECONDS------------582000
Explanation
In this example, the EXTRACT
function is used in conjunction with the SYSTIMESTAMP
function to obtain the current microsecond value from the system timestamp. This value is then given an alias Microseconds
for the sake of clarity. The FROM dual
clause is a standard feature in Oracle SQL for performing calculations, system calls, and other non-table operations.