Is there a function similar to Oracle's trunc (sysdate) in MySQL?

MySQLMySQLi Database

Yes, you can use DATE() to get only date part in MySQL and you can use CURDATE() to get the current date in MySQL.

The current date is as follows −

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-10-20 |
+------------+
1 row in set (0.00 sec)

Let us first create a table −

mysql> create table DemoTable1613
   -> (
   -> PostingDate datetime
   -> );
Query OK, 0 rows affected (0.48 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1613 values('2019-10-20 12:02:45');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1613 values('2018-10-20 12:02:45');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1613 values('2015-10-20 12:02:45');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1613 values('2019-10-20 10:00:00');
Query OK, 1 row affected (0.22 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1613;

This will produce the following output

+---------------------+
| PostingDate         |
+---------------------+
| 2019-10-20 12:02:45 |
| 2018-10-20 12:02:45 |
| 2015-10-20 12:02:45 |
| 2019-10-20 10:00:00 |
+---------------------+
4 rows in set (0.00 sec)

Here is the query to implement a function similar to Oracle's trunc (sysdate) in MySQL −

mysql> select * from DemoTable1613 where date(PostingDate)=curdate();

This will produce the following output

+---------------------+
| PostingDate         |
+---------------------+
| 2019-10-20 12:02:45 |
| 2019-10-20 10:00:00 |
+---------------------+
2 rows in set (0.00 sec)
raja
Published on 17-Dec-2019 10:43:01
Advertisements