Increment a date by a interval from a difference of dates - Postgres - postgresql

Edited: Added information that date1 is a timestamp
I need to increment date but the interval is based on the difference between the date that I want to increment and another date on the table but preserving date1 hours and minutes. E.g.:
Create myTable
( id serial,
date1 timestamp,
date2 date
)
So, if date1 = '2020-01-10 10:30:00'and date2 = '2020-03-08' I need date1 to be '2020-03-08 10:30:00'
Update myTable set date1 = date1+interval (date2-date1)
The syntax demands single quotes between the value (i.e: date + interval '30 days'). How to wrap the result from (date2-date1) in single quotes? I tried many ways without success:
Update myTable set date1 = date1+interval chr(39)||cast((date2-date1) as varchar)|| ' days'|| chr(39)
Update myTable set date1 = date1+interval $$||cast((date2-date1) as varchar)|| ' days'||$$
Update myTable set date1 = date1+interval ''||cast((date2-date1) as varchar)|| ' days'||''
How to I do this properly?

What you want is to set the date part of date1 to date2 and keep the time part:
update myTable
set date1 = date2 + date1::time;
See the demo.

Related

Updating date field with min date from selected dates if they are >= to current_date, except when all dates are >= to current date

I would like to update the contents of the Date1 column to reflect the oldest date in each row, unless the date has already passed (Date1 < current date), in which case i'd like Date1 to be populated with the 2nd oldest date in the row.
ID
Date 1
Date 2
Date 3
Date 4
001
01/14/2022
01/14/2022
01/15/2022
01/16/2022
002
04/15/2019
04/15/2019
01/10/2021
01/10/2021
I am currently using
update mytable t
set date1 = (
select min(date)
from (values (date2), (date3), (date4)) d(dt)
where dt >= current_date
)
The only problem I run into is when all available dates are prior to the current date. In this case it overwrites the value in the date1 column with null, which is not ideal. I'd like the query to leave the date1 field intact in these instances.
Figured it out:
update mytable t
set date1 = coalesce ((
select min(date)
from (values (date2), (date3), (date4)) d(dt)
where dt >= current_date
), date 1);

DB2 get max date of four dates query

I want to get greater date from four date columns and fields can be NULL.
Please help me to write a query for this.
Example:
select max(date1, date2, date3, date4) from table A where tag_id='xxxxx'
You can work with unions which combine your query results and then get the max date of the combined columns
Here is the same question asked in another post
Hope that helps you
select max(max_date) from
(select date1 max_date from table A where tag_id='xxxxx'
union all
select date2 from table A where tag_id='xxxxx'
union all
select date3 from table A where tag_id='xxxxx'
union all
select date4 from table A where tag_id='xxxxx')
Here you have:
SELECT
CASE
WHEN date1 >= date2 AND date1 >= date3 THEN date1
WHEN date2 >= date1 AND date2 >= date3 THEN date2
WHEN date3 >= date1 AND date3 >= date2 THEN date3
ELSE date1
END AS RecentDate
FROM table A
WHERE A.tag_id='xxxxx'
Hope it helps,
Did you try your example query?
MAX() (and MIN() for that matter) are a bit unusual....they exists as both an aggregate function operating on multiple rows and a scalar function operating on multiple columns.
The works fine for me on DB2 for IBM i:
select max(dte1,dte2,dte3,dte4) from qtemp.test

How to select specific dates in PostgreSQL?

My table:
create table example
(
code varchar(7),
date date,
CONSTRAINT pk_date PRIMARY KEY (code)
);
Dates:
insert into example(code, date)
values('001','2016/05/12');
insert into example(code, date)
values('002','2016/04/11');
insert into example(code, date)
values('003','2017/02/03');
My problem: how to select the previous dates to six month from today ?
In MySQL I can use PERIOD_DIFF,but, in PostgreSQL?
You can try INTERVAL instruction :
SELECT date
FROM example
WHERE date < CURRENT_DATE + INTERVAL '6 months'
AND date > CURRENT_DATE;
You will get the dates from today to six months.

Postgres: Update date and retain time from timestamp

I have a field1 with timestamp, datatype and values format is 2016-02-23 12:01:30.
I'm running the query:
UPDATE <table> set field1 = '2015-12-31'::timestamp::date where .....
The output changes to:
2015-12-31 00:00:00
It converts the time to all zero's. How to change the date and retain the timestamp?
Try this:
UPDATE mytable
SET field1 = '2015-12-31'::timestamp +
EXTRACT(HOUR FROM field1) * INTERVAL '1 HOUR' +
EXTRACT(MINUTE FROM field1) * INTERVAL '1 MINUTE' +
EXTRACT(SECOND FROM field1) * INTERVAL '1 SECOND'
WHERE ...
Demo here
There's another way, using the DateTime type.
So if you want to set a table's Date to today, you can use this:
UPDATE table SET column = current_date::date + column::time;
Switch current_date with "2019-02-23" and it should work as well.
Simply add the new date and the existing time. Here it is:
UPDATE mytable
SET field1 = '2015-12-31'::date + field1::time with time zone
WHERE ...;
or even cleaner cut:
UPDATE mytable
SET field1 = field1 - field1::date + '2015-12-31'::date
WHERE ...;
The subtraction of timestamps yields an interval. The resulting interval is can the be added to the desired date to give the desired date with the prior time.
with ats (old_tz) as (select now() )
select old_tz, '2015-12-31'::timestamptz + (old_tz - date_trunc('day', old_tz)) new_tz
from ats;
OOPS. Didn't realize how old this post was until after posting, but still believe it may valuable to future viewers. So I'll just leave it.

How do I convert a date ( YYYY-MM-DD ) into a month number in postgresql?

I got a table:
CREATE TABLE TRANSACTION (
transaction_date date,
id_transaction int,
PRIMARY KEY (id_transaction)
);
and I want to compare the month of 'transaction_date' field with a number of month.
SELECT *
FROM TRANSACTION T
WHERE month = transaction_date;
but I don't know how to make this conversion.
You can use EXTRACT(MONTH FROM transaction_date)
SELECT *
FROM transaction
WHERE EXTRACT(MONTH FROM transaction_date) = 1;
sqlfiddle demo
As per the documentation:
EXTRACT (field FROM source)
The extract function retrieves subfields such as year or hour from
date/time values. source must be a value expression of type timestamp,
time, or interval.
SELECT *
FROM TRANSACTION T
WHERE EXTRACT(MONTH FROM TIMESTAMP transaction_date) = month;
month should be an integer between 1 (January) and 12 (December).