MySQL NOW() Function with Example

MySQL NOW() Function with Example

MySQL NOW() function; In this tutorial, we will learn MySQL now() function with the help of examples.

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 in query. 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-3 | 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
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.

Recommended MySQL Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *