TIMESTAMP() Function Examples – MySQL

TIMESTAMP() Function Examples – MySQL

In this MySQL timestamp() function tutorial, we are going to discuss how to works the timestamp() function. How to use this function with different date and time functions.

MySQL TIMESTAMP Function

In MySQL, the TIMESTAMP() function is used to returns a DateTime value based on the passed argument in the function.

Note: You can pass one or two argument. If you provide two, it adds the second one to the first and returns the result.

Syntax

The syntax of this timestamp() function is:

TIMESTAMP(expr)
==========================
TIMESTAMP(expr1,expr2)

The first argument (expr and expr1) is a date or datetime expression. If you provide two agruments in this function, in that case, expr is added to exp1.

Example-1

Let’s take a simple example of the mysql timestamp() function. See the below:

SELECT TIMESTAMP('2019-07-21');

Output-1

+-------------------------+
| TIMESTAMP('2019-07-21') |
+-------------------------+
| 1999-12-31 00:00:00     |
+-------------------------+

Example-2

Let’s take second example of this function with date and time value. See the below:

SELECT TIMESTAMP('2019-07-21 23:59:59');

Result:

+----------------------------------+
| TIMESTAMP('2019-07-21 23:59:59') |
+----------------------------------+
|  2019-07-21 23:59:59             |
+----------------------------------+

Example-3

We take another example with factional seconds. If you want to use fractional seconds part up to microseconds (6 digits). You can do it. See the example below:

SELECT TIMESTAMP('2019-07-21 23:59:59.999999');

Output-3

+-----------------------------------------+
| TIMESTAMP('2019-07-21 23:59:59.999999') |
+-----------------------------------------+
|  2019-07-21 23:59:59.999999             |
+-----------------------------------------+

Example-4

Now let us take an example using two arguments. As mentioned, the second is added in the first place.

SELECT TIMESTAMP('2019-10-31', '12:30:45');

Output-4

+-------------------------------------+
| TIMESTAMP('2019-10-31', '12:30:45') |
+-------------------------------------+
|  2019-10-31 12:30:45                |
+-------------------------------------+

Example-5

Next, we take example of this function with curdate() function and now() function. It return to current timestamp.

    SELECT 
    TIMESTAMP(CURDATE()) AS 'Today',
    TIMESTAMP(NOW()) AS 'NOW', 
    TIMESTAMP(CURDATE(), '24:00:00') AS 'Tomorrow';

Result:

+---------------------+---------------------+---------------------+ 
| Today               |     NOW             | Tomorrow            |
+---------------------+---------------------+---------------------+ 
| 2019-07-21 00:00:00 | 2019-07-21 05:10:00 | 2019-07-22 00:00:00 |
+---------------------+---------------------+---------------------+ 

Conclusion

Here, you have learned how to use MySQL timediff() function with various examples.

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

Leave a Reply

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