Home » Enterprise Java » sql » SQL TIMESTAMP() function Example

About 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).

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments