SQL TIMESTAMP() function Example
Welcome readers, in this tutorial, we will learn how to use the TIMESTAMP()
function in SQL.
1. Introduction
In SQL, the TIMESTAMP()
function enables developers to deal with date-time expressions.
- The
TIMESTAMP()
function in SQL enables us to return a date expression from the Date or DateTime expression - The
TIMESTAMP()
function expects Date or DateTime value expression to return the DateTime value as the output. Syntax:TIMESTAMP(date or datetime expression)
- Syntax:
TIMESTAMP(expression, interval)
function an optional time value to add the expression
To start with this tutorial, I am hoping that users at present have their preferred database installed on their machines. For easy usage, I am using MySQL on a Windows operating system. If someone needs to go through the MySQL installation, please watch this video.
2. SQL TIMESTAMP() function Example
The following tutorial will illustrate the different syntax for the TIMESTAMP()
function.
2.1 Passing a Date expression to the TIMESTAMP() function
Readers can use the following SQL command to pass the date expression.
Query 1
1 | SELECT TIMESTAMP ( '2020-08-15' ); |
If everything goes well, the TIMESTAMP()
adds the default time expression and returns the DateTime value as the output.
2.2 Passing a DateTime expression and Time expression to the TIMESTAMP() function
Readers can use the following SQL command to pass the DateTime and Time expression.
Query 2
1 | SELECT TIMESTAMP ( '2020-08-15 15:30:00' , '5:00:00' ); |
If everything goes well, the TIMESTAMP()
adds the time values of both the expressions and returns the DateTime value of it.
That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this section, we learned:
- How to use the
TIMESTAMP()
function - Steps required to set up the MySQL database on a Windows operating system
You can download the sample script in the Downloads section.
4. Download the SQL Script
This was an example of TIMESTAMP()
function in SQL.
You can download the full source code of this example here: SQL TIMESTAMP() function Example