Tutorial

SQL TIMESTAMP() Function - All you need to know!

Published on August 4, 2022
author

Safa Mulani

SQL TIMESTAMP() Function - All you need to know!

Hey, all. In our series of SQL DATE and TIME functions, in this article, we will learn the SQL TIMESTAMP() function in detail.

The SQL DATE and TIME functions enable us to deal date-time data. Moreover, using these functions, we can extract a portion of the date and time from the datetime expressions.

So, let us begin with understanding about the working of TIMESTAMP() function.


What is SQL TIMESTAMP() function?

TIMESTAMP represents the date and time value of a particular record at a particular event.

The TIMESTAMP() function enables us to represent and return a datetime expression out of a date or datetime expression. Moreover, it accepts the date expression and adds time value to it and returns the datetime expression.

Having understood the working of TIMESTAMP() function, let us now focus on the structure of the same.


Syntax of TIMESTAMP() function

The TIMESTAMP() function accepts the date or datetime expression as an argument and it returns the datetime value out of it as output.

Syntax:

TIMESTAMP(date or datetime expression)

The SQL TIMESTAMP() function also even accepts two date or datetime values as arguments to return the output as shown below:

The first argument gets added to the second argument and then the resultant datetime value is returned.

Syntax:

TIMESTAMP(expression1, expression2)

Now, let us implement the same through the below examples.


Examples of TIMESTAMP() function

In the below example, we have used the SELECT statement passed a date expression to the timestamp() function.

SELECT TIMESTAMP("2020-07-28")

The function adds the default time expression value to it and returns the datetime value.

Output:

2020-07-27T22:00:00.000Z

Now, we have passed a datetime expression to the function and the functions returns a datetime value out of it.

SELECT TIMESTAMP("2020-07-28 20:12:12")

Output:

2020-07-28T18:12:12.000Z

In the below example, we have passed a datetime expression and a time expression to the TIMESTAMP() function.

SELECT TIMESTAMP('2020-07-28 20:10:10','1:50:50');

It adds the time values of both the expressions and returns the datetime value of it.

Output:

2020-07-28T20:01:00.000Z

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to SQL, do visit SQL JournalDev.

Till then, Happy Learning!!


References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.