The NOW()
function in MySQL is a powerful tool that returns the current date and time based on the server's time zone. This function is widely used when you need to capture the exact moment an event occurs such as when recording timestamps for records like orders, deliveries, or log entries.
In this article, We will learn about the MYSQL NOW() function by understanding various examples in detail.
MySQL NOW() function
- The
NOW()
function in MySQL is used to return the current date and time based on the servers time zone.
- It provides the date and time in the format
YYYY-MM-DD HH:MM:SS
.
- The date and time returned by the NOW() function in MySQL are either in ‘YYYY-MM-DD HH:MM:SS’ format or 'YYYYMMDDHHMMSS.uuuuuu' format, depending on whether the function is used in a string or numeric context.
Syntax:
MySQL NOW function syntax is:
NOW()
Parameter
This method does not accept any parameters.
Examples of MySQL NOW() Function
Let's look at some examples of the NOW function in MySQL. Learning the MySQL NOW function with examples will help you understand the concept better.
Example 1:
In this example, we are finding the current date and time using the NOW function. Here we will get the result in ‘YYYY-MM-DD HH:MM:SS’ format.
Query:
SELECT NOW()
AS CurrDateTime ;
Output :
CURRDATETIME |
---|
2020-11-26 18:37:42 |
Example 2:
In this example, we are getting the current date and time using NOW Function in numeric format. Here the result will be in YYYYMMDDHHMMSS.uuuuuu format.
SELECT NOW()+ 0
AS CurrDateTime ;
Output :
CURRDATETIME |
---|
20201126183839 |
Example 3:
In this example, we use the NOW function to set the value of the columns.
First, let's create a table named DeliveryDetails.
CREATE TABLE Product (
ProductId INT NOT NULL,
ProductName VARCHAR(20) NOT NULL,
Delivered_At TIMESTAMP NOT NULL,
PRIMARY KEY(ProductId)
);
Here, we will use the NOW function to determine when a delivery will be completed. The value in Delivered_At column will be the value given by the NOW function.
INSERT INTO
Product (ProductId, ProductName, Delivered_At)
VALUES
(1010, 'Apple MacBook', NOW());
Now, check the Product table :
SELECT * FROM Product;
Output:
PRODUCTID | PRODUCTNAME | DELIVERED_AT |
---|
1010 | Apple MacBook | 2021-10-01 14:41:15 |
Conclusion
The MySQL NOW()
function is essential for capturing real-time data in your applications. Whether you are creating records with precise timestamps or performing time-sensitive operations, NOW()
ensures that your database accurately reflects the current moment. Use this function wisely, considering server time zone configurations, to achieve accurate and reliable results
Similar Reads
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
WEEK() Function in MySQL
WEEK() function in MySQL is a versatile built-in date function designed to extract the week number from a given date. This function is particularly beneficial for grouping and analyzing data based on weekly intervals, allowing for more insightful data interpretation and reporting.In this article, We
3 min read
MySQL | VERSION( ) Function
The MySQL Version() function is used for returning the current version of the MySQL database. This function uses the utf8 character set. Generally, there is a suffix returned after the version number. The Version() function does not require any parameter to be passed. Syntax: VERSION() Parameters Us
1 min read
SECOND() Function in MySQL
SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu
2 min read
MySQL | CAST( ) Function
The MySQL CAST() function is used for converting a value from one datatype to another specific datatype. The CAST() function accepts two parameters which are the value to be converted and the datatype to which the value needs to be converted. The datatypes in which a given value can be converted are
3 min read
MySQL | OLD_PASSWORD Function
The MySQL OLD_PASSWORD function is used for generating a hashed password from a plaintext password string. The OLD_PASSWORD function uses hashing techniques to perform the generation of the hashed password. This function is carried out by the authentication system. This function is used by the MySQL
1 min read
TIME() Function in MySQL
The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 min read
YEAR() Function in MySQL
YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999. Syntax : YEAR(date) Parameter : This method accepts one parameter as mentioned above and described below : date : The dat
3 min read
MySQL QUARTER() Function
MySQL QUARTER() function is used to return the quarter of the year for a given date value. QUARTER function in MySQL returns values between 1 to 4. For January to March, it returns 1, for April to June, it returns 2, For July to September, it returns 3, For October to December, it returns 4. SyntaxM
2 min read
SQRT() Function in MySQL
The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 min read