Today we are going to demonstrate MySQL now() function with various types of examples.
We will explore the MySQL now() function with MySQL queries to the database.
The MySQL NOW() is a very popular function and it is daily useable function. Mysql provide many date and time functions, you want to learn more about the date and time functions checkout this
MySQL NOW() Function
Basically MySQL NOW() is used to returns the current date and time. It will return the value of two types format, first is the YYYY-MM-DD HH:MM:SS and second is the YYYYMMDDHHMMSS format. But it depends on the function is used to string or numeric context.
Syntax
NOW([fsp])
Note: fsp is optional. It means fractional second precision.
Basic Example-1
Now we will take an example and explain it.
SELECT NOW();
Output-1
+---------------------+ | NOW() | +---------------------+ | 2019-07-14 11:17:58 | +---------------------+
Example-2 With FSP
Now we take another example with fsp argument or params.
SELECT NOW(3);
Output-2
+----------------------------+ | NOW(3) | +----------------------------+ | 2019-07-14 05:12:06.095048 | +----------------------------+
Example-4 | get current date data in MySQL
Now we take the next example with a database table. We take database table employees and get the current date data or record from the database table using MySQL NOW().
SELECT name, created_at
FROM employees
WHERE DATE(created_at) = DATE(NOW()) ORDER BY id
DESC
Output-3
The above Mysql query produces the result, see below :
Current-Date-Record-MySQL
Example-4
Now we take the next example with a numeric context with NOW().
SELECT NOW() + 2;
Output-4
+----------------+ | NOW() + 2 | +----------------+ | 20190714055004 | +----------------+
Example-5
We take another example of now() function when we used to numeric context with now() function. It will return the number value.
The below example, add 1 hour, minus -1 hour and add 1 day with current date and time.
SELECT (NOW() - INTERVAL 1 HOUR) 'NOW - 1 hour', NOW(), NOW() + INTERVAL 1 HOUR 'NOW + 1 hour';
Conclusion
Here, you have learned how to use MySQL NOW() function with various examples.