I have tried to use the following in PostgreSQL but it adds about 2000 years to the year. I am needing to change the year from 2096 to 2001 or 2000 doesn't really matter. When I run this it adds about 2000 years to the year 2096 and makes it 4096.
UPDATE module_warrants
SET receiveddate = receiveddate + MAKE_INTERVAL(YEARS :=2096 -96)
where receiveddate >= '01-01-2094'
Expect the year to decrease by 96 years to 2000 from 2096.
The expression + MAKE_INTERVAL(YEARS :=2096 -96) is adding 2000 years (2096-96) to the current value.
But you want to subtract the number:
receiveddate - make_interval(years => 96)
or
receiveddate - interval '96 years'
Related
I have 2 columns in my table firstdate and lastdate
firstdate is for example today (2020-1-22).
lastday is plus 1 years minus 1 day (2021-1-21).
So my insert script is like:
INSERT INTO <table> (firstdate, lastdate)
VALUES (current_date, current_date + interval '1 year -1 day')
Ok, so far so good. Now I want to extrapolate it to use it again.
My question is based on these 2 dates. How can i return the interval back again from these 2 dates?
When I use the Postgres function age, I get the following result: 0 years 11 months 30 days.
I can't use this interval to make the right calculation because interval 0 years 11 months 30 days is not the same as interval 1 years -1 days.
So somehow I need to get back 1 years -1 days
lastdate-firstdate gives you the interval between the dates. You cannot get back the literal string you used to create the original interval.
Best regards,
Bjarni
You can store the interval instead of lastdate then derive the lastdate when needed as column interval_value. So
select firstdate,first_date+interval_value lastdate ...
or if you have version 12, define lastdate as a virtual column. Then just select normally. Either way you have the necessary interval.
I need help on calculating my start date for my report date parameters.
The end date will always be the last Sunday, here: =DateAdd("d", 1 - WeekDay(Today(), 1), Today())
What I need help with is how to write a formula to go back 6 months from today and pick the 1st Saturday in that range..
Thanks in advance.
Assuming your start day is a Sunday then you can use this...
=DATEADD(
DateInterval.Day,
7 - WEEKEDAY( DATEADD(DateInterval.Month,-6,Today()), FirstDayOfWeek.Sunday),
DATEADD(DateInterval.Month,-6,Today())
)
This works as follows
WEEKEDAY( DATEADD(DateInterval.Month,-6,Today()), FirstDayOfWeek.Sunday)
Takes today's date, subtracts 6 months and then finds out what daynumber that is. Running that today (2018-11-08) gives use (2018-05-10) which is a Thursday, this is day number 5
Saturdays are day number 7 (if your first day of week is a Sunday). As there can be no higher number than 7 we can do a simple subtraction of 7 minus the day number we landed on (from above) which gives us a required adjustment of 2 days.
Finally the outer DATEADD function simply says add our calculated 2 days to the date 6 months ago.
Hope that makes sense!?
If the first day of the week is not a Sunday for you then you may have to do some Mod% calc on the second argument to calculate the correct number of days to adjust by.
I've tried to subtract interval from timestamp, but I've got a wrong result in comparison to days via subtracting 2 dates.
E.g.:
select
(now::date - past::date) as days,
(now::date - past::date) / 365.25 as years,
justify_interval(now - past::date) as interval_test
from (
select '2020-09-17 00:00:01'::timestamp as now, '2010-09-17 00:00:01'::timestamp as past
) b;
gives results:
3653 days
10.0013 years
'10 years 1 mon 23 days' interval test
Could anyone help me to understand what is wrong with subtracting?
When I do it vice versa, it's ok:
select
(past::date + 3653)::date,
(past + interval '10 years')::date,
(past + 10*interval '1 year')::date,
(past + 10*12*interval '1 month')::date
from (
select '2020-09-17 00:00:01'::timestamp as now, '2010-09-17 00:00:01'::timestamp as past
) b;
all results give the same date '2020-09-17'
What I do wrong?
I am using PostgreSQL 10.5.
There is nothing wrong with subtracting. It is just that justify_interval doesn't do what you seem to expect. justify_interval uses 30 day months and 24 hour days. So 12 months becomes only 360 and 10 years only 3600 days. Leaving 53 days which is 1 (30 day) month and 23 days.
Edit
The justify_interval documentation on this page refers to justify_days and justify_hours which are directly above it which do mention the use of 30 days months and 24 hour days.
The justify functions do have to make these assumption because the interval type is a general length of time (it has no specific start and end). So the justify functions does not know over which specific months the interval was originally calculated.
The age function however does not take an interval it takes an end and a start so it actually knows which specific months and years are in that period.
For some reason justify_interval(now() - '2013-02-14'::timestamptz) produces weird results:
postgres=# select justify_interval(concat(365*4 +1,' days')::interval); -[ RECORD 1 ]----+----------------
justify_interval | 4 years 21 days
I checked one year:
postgres=# select justify_interval('365 days'::interval);
justify_interval
------------------
1 year 5 days
So I went further:
postgres=# select justify_interval('360 days'::interval);
justify_interval
------------------
1 year
(1 row)
This behavior is not platform specific (tried several Linuxes, 9.2, 9.3, 9.6)
Why one year is 360 days?..
It seems that you are looking for something, which PostgreSQL calls a "symbolic" result that uses years and months, rather than just days, which is what the age(timestamp, timestamp) (and age(timestamp)) function(s) returns.
select age(now(), '2013-02-14'); -- 4 years 16:41:02.571547
select age(timestamp '2013-02-14'); -- 4 years
The - operator always returns the difference in days (at most). The justify_*() functions (and the *, /, <, > operators) always "cut" values to an average (i.e. 1 day is 24 hours and 1 month is 30 days) despite the fact that 1 day actually can contain 23-25 hours (just think of daylight saving time zones) and 1 month can contain 28-31 days (so the result depends on the actual start and end points of the range, which creates the interval).
accrding to docs:
justify_interval(interval) - Adjust interval using justify_days and
justify_hours, with additional sign adjustments
and further:
justify_days(interval) - Adjust interval so 30-day time periods
are represented as months
So 30*12=360
Not expected but obviously defined in docs...
Date(04-04-2013) - 10 month ( must give date which should be 06-04-2012)
I need to get data between previous 10 months to current date i.e. April.
The year will change -1, so getting problem.
I'm not really sure what your question is exactly, but I think you just want to get data from a table between now and 10 months ago. This should do it:
SELECT *
FROM your_table
WHERE your_date BETWEEN (CURRENT DATE - 10 MONTHS) AND (CURRENT DATE)
You don't have to worry about it being a different year, because the date calculation (- 10 MONTHS) will handle all of that.