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.
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.
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.
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
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!!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.