Remove seconds from MySQL Datetime?

MySQLMySQLi Database

To remove seconds from MySQL datetime, use UPDATE and SET as in the following syntax −

update yourTableName set yourDateColumnName=yourDateColumnName -second(yourDateColumnName);

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Shippingdatetime datetime
   -> );
Query OK, 0 rows affected (0.76 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-01-21 12:05:20');
Query OK, 1 row affected (0.25 sec)

mysql> insert into DemoTable values('2019-03-01 10:23:35');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('2019-06-09 11:00:56');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+---------------------+
| Shippingdatetime    |
+---------------------+
| 2019-01-21 12:05:20 |
| 2019-03-01 10:23:35 |
| 2019-06-09 11:00:56 |
+---------------------+
3 rows in set (0.00 sec)

Here is the query to remove seconds from MySQL datetime −

mysql> update DemoTable set Shippingdatetime=Shippingdatetime-second(Shippingdatetime);
Query OK, 3 rows affected (0.15 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check all records from the table once again −

mysql> select *from DemoTable;

Output

+---------------------+
| Shippingdatetime    |
+---------------------+
| 2019-01-21 12:05:00 |
| 2019-03-01 10:23:00 |
| 2019-06-09 11:00:00 |
+---------------------+
3 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 15:12:59
Advertisements