Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL MICROSECOND() function

MICROSECOND() function

MySQL MICROSECOND() returns MICROSECONDs from the time or datetime expression. The return value is within the range of 0 to 999999.

Syntax:

MICROSECOND(expr)

Where expr is a time or datetime.

Syntax Diagram:

MySQL MICROSECOND() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation:

Pictorial Presentation:

Pictorial Presentation of MySQL MICROSECOND() function

Example: MySQL MICROSECOND() function

The following statement will return MICROSECONDs from the datetime 2009-05-18 10:15:21.000423.

Code:

SELECT MICROSECOND('2009-05-18 10:15:21.000423');

Sample Output:

mysql> SELECT MICROSECOND('2009-05-18 10:15:21.000423');
+-------------------------------------------+
| MICROSECOND('2009-05-18 10:15:21.000423') |
+-------------------------------------------+
|                                       423 | 
+-------------------------------------------+
1 row in set (0.00 sec)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-MICROSECOND-function - php mysql examples | w3resource</title>
<meta name="description" content="example-MICROSECOND-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Extract MICROSECONDs from 2009-05-18 10:15:21.000423:</h2>
<table class='table table-bordered'>
<tr>
<th>MICROSECOND('2009-05-18 10:15:21.000423')</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT MICROSECOND("2009-05-18 10:15:21.000423")') as $row) {
echo "<tr>";
echo "<td>" . $row['MICROSECOND("2009-05-18 10:15:21.000423")'] . "</td>"; 
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-microsecond-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT MICROSECOND('2009-05-18 10:15:21.000423')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">

<tr width="10" bgcolor="#9979">
<td>MICROSECOND('2009-05-18 10:15:21.000423')</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("MICROSECOND('2009-05-18 10:15:21.000423')")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example: MICROSECOND() function with DATE_FORMAT()

The following statement will return MICROSECONDs from a datetime which will come from the CURRENT_TIMESTAMP() and will format it as a datetime expression by DATE_FORMAT() as given in the argument (%Y-%c-%d %H:%i:%s %f) .

Code:

SELECT MICROSECOND(DATE_FORMAT( CURRENT_TIMESTAMP(),'%Y-%c-%d %H:%i:%s %f'));

Sample Output:

mysql> SELECT MICROSECOND(DATE_FORMAT( CURRENT_TIMESTAMP(),'%Y-%c-%d %H:%i:%s %f'));
+-----------------------------------------------------------------------+
| MICROSECOND(DATE_FORMAT( CURRENT_TIMESTAMP(),'%Y-%c-%d %H:%i:%s %f')) |
+-----------------------------------------------------------------------+
|                                                                     0 | 
+-----------------------------------------------------------------------+
1 row in set, 1 warning (0.01 sec)

PHP script:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-MICROSECOND-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-MICROSECOND-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Extract MICROSECONDs from current datetime:</h2>
<table class='table table-bordered'>
<tr> 
<th>MICROSECOND(DATE_FORMAT(CURRENT_TIMESTAMP(),'%Y-%c-%d %H:%i:%s %f'))</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT MICROSECOND(DATE_FORMAT(
CURRENT_TIMESTAMP(),"%Y-%c-%d %H:%i:%s %f"))') as $row) {
echo "<tr>"; 
echo "<td>" . $row['MICROSECOND(DATE_FORMAT(
CURRENT_TIMESTAMP(),"%Y-%c-%d %H:%i:%s %f"))'] . "</td>";
echo "</tr>";  
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

All Date and Time Functions :

Click here to see the MySQL Date and time functions.

Previous: MAKETIME()
Next: MINUTE()