
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert Current Date to Database in MySQL
To insert current date to the database, you can use NOW(). Following is the syntax −
INSERT INTO yourTableName(yourDateColumnName) VALUES(NOW());
If your column has datatype date then NOW() function inserts only current date, not time and MySQL will give a warning. To remove the warning, you can use CURDATE().
Let us first create a table −
mysql> create table insertcurrentdate -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> currentDate date -> ); Query OK, 0 rows affected (1.09 sec)
Following is the query to insert some records in the table using insert command. We have used both NOW() and CURDATE() to display current date −
mysql> insert into insertcurrentdate(currentDate) values(NOW()); Query OK, 1 row affected, 1 warning (0.24 sec) mysql> insert into insertcurrentdate(currentDate) values(CURDATE()); Query OK, 1 row affected (0.18 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from insertcurrentdate;
This will produce the following output −
+----+-------------+ | Id | currentDate | +----+-------------+ | 1 | 2019-04-05 | | 2 | 2019-04-05 | +----+-------------+ 2 rows in set (0.00 sec)
Advertisements