Let us go through some of the important date manipulation functions.
-
Getting Current Date and Timestamp
-
Date Arithmetic using INTERVAL
and -
operator
-
Getting beginning date or time using date_trunc
-
Extracting information using to_char
as well as calendar functions.
-
Dealing with unix timestamp using from_unixtime
, to_unix_timestamp
Getting Current Date and Timestamp
Let us understand how to get the details about current or today’s date as well as current timestamp.
The sql extension is already loaded. To reload it, use:
%reload_ext sql
env: DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db
-
current_date
is the function or operator which will return today’s date.
-
current_timestamp
is the function or operator which will return current time up to milliseconds.
-
These are not like other functions and do not use () at the end.
-
There is a format associated with date and timestamp.
-
We can apply all string manipulation functions on date or timestamp once they are typecasted to strings using varchar
.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
current_timestamp |
2020-12-01 10:55:19.250677+00:00 |
Note
Example of applying string manipulation functions on dates. However, it is not a good practice. Postgres provide functions on dates or timestamps for most of the common requirements.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
Date Arithmetic
Let us understand how to perform arithmetic on dates or timestamps.
-
We can add or subtract days or months or years from date or timestamp by using special operator called as INTERVAL
.
-
We can also add or subtract hours, minutes, seconds etc from date or timestamp using INTERVAL
.
-
We can combine multiple criteria in one operation using INTERVAL
-
We can get difference between 2 dates or timestamps using minus (-
) operator.
The sql extension is already loaded. To reload it, use:
%reload_ext sql
env: DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2021-01-02 00:00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2022-12-01 00:00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2018-12-02 00:00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2018-12-02 00:00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2021-03-01 00:00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2019-04-30 00:00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2019-05-03 03:00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2021-03-01 10:55:19.336241+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2020-12-01 20:55:19.343569+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2020-12-01 11:05:19.350628+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
2020-12-01 21:05:19.357712+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
result |
612 days, 10:55:19.384205 |
Beginning Date or Time - date_trunc
Let us understand how to use date_trunc
on dates or timestamps and get beginning date or time.
The sql extension is already loaded. To reload it, use:
%reload_ext sql
env: DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
year_beginning |
2020-01-01 00:00:00+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
month_beginning |
2020-12-01 00:00:00+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
week_beginning |
2020-11-30 00:00:00+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
day_beginning |
2020-12-01 00:00:00+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
hour_beginning |
2020-12-01 10:00:00+00:00 |
Dealing with Unix Timestamp
Let us go through the functions that can be used to deal with Unix Timestamp.
-
extract
with epoch
can be used to convert Unix epoch to regular timestamp. We can also use date_part
;
-
to_timestamp
can be used to convert timestamp to Unix epoch.
-
We can get Unix epoch or Unix timestamp by running date '+%s'
in Unix/Linux terminal
Let us sww how we can use functions such as extract
or to_timestamp
to convert between timestamp and Unix timestamp or epoch.
The sql extension is already loaded. To reload it, use:
%reload_ext sql
env: DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
time_from_epoch |
2019-04-30 22:18:51+00:00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
time_from_epoch |
2019-04-30 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.