PostgreSQL TIMESTAMP() Function
The PostgreSQL TIMESTAMP() function returns a datetime value based on a date or datetime value. It converts an expression to a datetime value and if specified adds an optional time interval to the value.
Syntax
TIMESTAMP(expression, interval)
Parameters
expression |
Required. Specify a date or datetime value. |
interval |
Optional. Specify a time value to add to expression. |
Return Value
Returns a datetime value.
Example 1:
The example below shows the usage of TIMESTAMP() function.
SELECT TIMESTAMP('2019-10-25'); Result: '2019-10-25 00:00:00' SELECT TIMESTAMP('2019-10-25', '01:10:25'); Result: '2019-10-25 01:10:25' SELECT TIMESTAMP('2019-10-25 08:40:45', '01:10:25'); Result: '2019-10-25 09:51:10' SELECT TIMESTAMP('2019-10-25 08:40:45.001234', '01:10:25.001000'); Result: '2019-10-25 09:51:10.002234'
Example 2:
Consider a database table called EmployeeLogin with the following records:
EmpID | Name | Login Stamp | Expected Logout Stamp |
---|---|---|---|
1 | John | 2019-10-25 09:20:38 | 2019-10-25 17:50:38 |
2 | Marry | 2019-10-25 09:21:05 | 2019-10-25 17:51:05 |
3 | Jo | 2019-10-25 09:24:35 | 2019-10-25 17:54:35 |
4 | Kim | 2019-10-25 09:25:24 | 2019-10-25 17:55:24 |
5 | Ramesh | 2019-10-25 09:27:16 | 2019-10-25 17:57:16 |
To insert a new record in this table, the following statement can be used.
INSERT INTO EmployeeLogin VALUES (6, 'Suresh', NOW(), TIMESTAMP(NOW(), '08:30:00')); -- see the result SELECT * FROM EmployeeLogin;
This will produce a result similar to:
EmpID | Name | Login Stamp | Expected Logout Stamp |
---|---|---|---|
1 | John | 2019-10-25 09:20:38 | 2019-10-25 17:50:38 |
2 | Marry | 2019-10-25 09:21:05 | 2019-10-25 17:51:05 |
3 | Jo | 2019-10-25 09:24:35 | 2019-10-25 17:54:35 |
4 | Kim | 2019-10-25 09:25:24 | 2019-10-25 17:55:24 |
5 | Ramesh | 2019-10-25 09:27:16 | 2019-10-25 17:57:16 |
6 | Suresh | 2019-10-25 09:28:19 | 2019-10-25 17:58:19 |
❮ PostgreSQL Functions