MySQL - TIMESTAMP() Function



The MySQL TIMESTAMP() function is used to convert the date or datetime expression as a datetime value and returns the result in the form of a string.

This function accepts two expressions as arguments. If only a single expression is passed as an argument, this function returns the date or datetime expression as a datetime value.

We can also pass the a second argument which represents a time expression. If you do so the specified time expression is added to the date given.

A NULL value is returned if either of any expression passed as an argument is NULL.

Syntax

Following is the syntax of MySQL TIMESTAMP() function −

TIMESTAMP(expr)

Parameters

This method accepts an expression that represents a date or datetime value as a parameter.

Return value

This function returns a timestamp value in the format 'YYYY-MM-DD HH:MM:SS'.

Example

In the following query, we are using the MySQL TIMESTAMP() function to convert the provided string into a timestamp format −

SELECT TIMESTAMP('2023:11:17') As Result;

Output

This will produce the following result −

Result
2023-11-17 00:00:00

Example

We can also pass the date-time expression as an argument to this function −

SELECT TIMESTAMP('2023-11-17 09:40:45.2300') As Result;

Output

Following is the output −

Result
2023-11-17 09:40:45.2300

Example

In the following query, we are adding a time value to the current timestamp −

SELECT TIMESTAMP(CURRENT_TIMESTAMP, '12:12:12') As Result;

Output

Following is the output −

Result
2023-11-18 02:24:06

Example

Here, we are passing the result of the CURTIME() function as the second argument to this function −

SELECT TIMESTAMP('2023:11:17', CURTIME()) As Result;

Output

Following is the output −

Result
2023-11-17 14:12:23

Example

In this example, we have created a table named ORDERS using the following CREATE TABLE query −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

Now, insert the following records into the ORDERS table using the INSERT statement −

INSERT INTO ORDERS VALUES 
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);

Execute the below query to fetch all the inserted records in the above-created table −

Select * From ORDERS;

Following is the ORDERS table −

OID DATE CUSTOMER_ID AMOUNT
102 2009-10-08 00:00:00 3 3000.00
100 2009-10-08 00:00:00 3 1500.00
101 2009-11-20 00:00:00 2 1560.00
103 2008-05-20 00:00:00 4 2060.00

Here, we are using the MySQL TIMESTAMP() function to add the specified time value (12:00:00) to the datetime values in the "DATE" column −

SELECT OID, DATE, TIMESTAMP(DATE, '12:00:00')
As Result FROM ORDERS;

Output

The output is displayed as follows −

OID DATE Result
102 2009-10-08 00:00:00 2009-10-08 12:00:00.000000
100 2009-10-08 00:00:00 2009-10-08 12:00:00.000000
101 2009-11-20 00:00:00 2009-11-20 12:00:00.000000
103 2008-05-20 00:00:00 2008-05-20 12:00:00.000000
Advertisements