Format MySQL CURRENT_TIMESTAMP to AM & PM?

MySQLMySQLi Database

To format, use DATE_FORMAT(). Let us first create a table

mysql> create table DemoTable
(
   LoginTime time
);
Query OK, 0 rows affected (0.48 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('13:10');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('20:08');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values('10:55');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('16:40');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+
| LoginTime |
+-----------+
| 13:10:00  |
| 20:08:00  |
| 10:55:00  |
| 16:40:00  |
+-----------+
4 rows in set (0.00 sec)

Following is the query to format CURRENT_TIMESTAMP to AM & PM −

mysql> select date_format(LoginTime,'%r') from DemoTable;

This will produce the following output −

+-----------------------------+
| date_format(LoginTime,'%r') |
+-----------------------------+
| 01:10:00 PM                 |
| 08:08:00 PM                 |
| 10:55:00 AM                 |
| 04:40:00 PM                 |
+-----------------------------+
4 rows in set (0.00 sec)
raja
Published on 25-Sep-2019 13:49:41
Advertisements