I have a 'test_date' column in 'test' table in Postgres DB, ex - (2018-05-29)
I need to calculate difference between current date and that date column and return the result as days, months, years.
I tried -
select (current_date - test_date) from test;
but it returns the values as days. but I need the result as days, months, years.
How to convert it properly ?
The age() function returns the value as an interval rather than the number of days:
select age(current_date, test_date)
If you use a timestamp then you'll get a `interval' back:
select justify_interval(date_trunc('day', current_timestamp) - test_date)
The date_trunc() is there to set the time part of the timestamp to 00:00:00. By default that would return an interval with only days in it. The justify_interval() will then "normalize" this to months, weeks and days.
E.g. 0 years 7 mons 28 days 0 hours 0 mins 0.0 secs
Related
I am using the DATEDIFF function to calculate the difference between my two timestamps.
payment_time = 2021-10-29 07:06:32.097332
trigger_time = 2021-10-10 14:11:13
What I have written is : date_diff('minute',payment_time,trigger_time) <= 15
I basically want the count of users who paid within 15 mins of the triggered time
thus I have also done count(s.user_id) as count
However it returns count as 1 even in the above case since the minutes are within 15 but the dates 10th October and 29th October are 19 days apart and hence it should return 0 or not count this row in my query.
How do I compare the dates in my both columns and then count users who have paid within 15 mins?
This also works to calculate minutes between to timestamps (it first finds the interval (subtraction), and then converts that to seconds (extracting EPOCH), and divides by 60:
extract(epoch from (payment_time-trigger_time))/60
In PostgreSQL, I prefer to subtract the two timestamps from each other, and extract the epoch from the resulting interval:
Like here:
WITH
indata(payment_time,trigger_time) AS (
SELECT TIMESTAMP '2021-10-29 07:06:32.097332',TIMESTAMP '2021-10-10 14:11:13'
UNION ALL SELECT TIMESTAMP '2021-10-29 00:00:14' ,TIMESTAMP '2021-10-29 00:00:00'
)
SELECT
EXTRACT(EPOCH FROM payment_time-trigger_time) AS epdiff
, (EXTRACT(EPOCH FROM payment_time-trigger_time) <= 15) AS filter_matches
FROM indata;
-- out epdiff | filter_matches
-- out ----------------+----------------
-- out 1616119.097332 | false
-- out 14.000000 | true
I want a query that shows a time difference in months or days in Impala
How can I do this?
start 2017-11-29 19:45:00 - end 2018-11-29 21:30:00
I know that month_between and datediff shows the month of datediff but how do I make it so it also takes the year into count when counting the days / months?
For the above example, I want to to display either
month_between - 12.2 months - equivalent to the month calculation of the timestamp - might be a little off cause I did it by hand / 30 days
days_between - 366 days
not sure if you tried DATEDIFF or not , but it already gives you the dates difference in days :
select datediff(endddaate, startdate)
from tablename
I want to calculate the difference between two dates in days such that for hours from 1-23 we consider it as a day.
for example: Date1 = '2021-06-15 01:52:00.926+00'
Date2 = '2021-06-15 02:52:00.926+00'
Here, Date1-Date2 = 1hour. I want to take ceil of it to be 24hrs i.e 1 day.
I tried (Date2::DATE - Date1::DATE) but it gives 0.
There can be two scenarios: If difference between days is 35hrs it should return 3(i.e 3 days). If the difference between dates is 5hrs it should return 1(i.e 1 day).
You can use epoch arithmetic:
select ceiling(extract(epoch from date2) - extract(epoch from date1)) / (24 * 60 * 60)
from t;
Note that this particular formulation counts anything longer than 1 day as 2 days. I think that is the intention of your question. However, if you really do have a 1 hour buffer, the logic could be tweaked to handle that.
Not technically an answer to your PostgreSQL question, however in Ingres (Postgres was the successor to Ingres) we have a interval function which returns the interval between two dates. Do you have anything similar in PostgreSQL?
e.g. select interval('hours', date1 - date2)
In BigQuery, I have a table of values with one column containing dates in YYYY-MM-DDtHH-MM-SS format, for example, 2020-07-24T20:13:35.
I want to pull only the rows from the past 30 days and exclude any rows that are more than 30 days old.
I believe I found out how to do it for date formatted as YYYY-MM-DD:
(Column name is "dates")
SELECT DATE_SUB(dates, INTERVAL 30 DAY)
This does not work when it is formatted as YYYY-MM-DDtHH-MM-SS though.
You would simply use:
where col > datetime_add(current_datetime, interval -30 day)
or
where col > timestamp_add(current_timestamp, interval -30 day)
depending on whether the column is a datetime or timestamp.
You can also use
Select * from table where date(date_column) >= date_sub(current_date, interval 30 day)
This way you will get all the records from 30 days before, starting from 12am. Using current_datetime or current_timestamp will only give results later than 30 days ago at time you run your query.
How would I count the days from a date till the first of the following month
Example:
--Start Date
07-07-2011
How many days till:
-- The 1st of the succeeding month of the start date above
08-01-2011
Expected Result (in days):
25
So if I counted the day I get 25, so running this query gets me the desired timestamp:
SELECT CURRENT_DATE + INTERVAL '25 DAYS'
Results:
2011-08-01 00:00:00
just can't think of a way to get the number of days, any suggestions?
Or start date, end date, number of days between?
I don't have a PostgreSQL server handy, so this is untested, but I would try:
SELECT (DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 MONTH') - CURRENT_DATE