sql

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.

SQL TIMESTAMP
Fig. 1: TIMESTAMP() expression 1

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.

SQL TIMESTAMP
Fig. 2: TIMESTAMP() expression 2

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.

Download
You can download the full source code of this example here: SQL TIMESTAMP() function Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button