So I am trying to have a select statement within an INTERVAL in postgres
UPDATE v_obligation
SET current_alert_level=0,
last_update_date=now()AT TIME ZONE 'PST',
next_due_date = (now()+INTERVAL ''(
SELECT recurrence FROM v_obligation
WHERE obligation_id=4) Day'')
WHERE obligation_id = 4
But I am getting a syntax error in the nested query. I can't recall doing anything like this before. As you can see I tried escaping the quotes, but to no avail.
Sincere thanks for any help... It is greatly appreciated!
There is a simple way to convert integer to interval, eg.
select now()+ (select 5) * '1 day'::interval
So try this:
UPDATE v_obligation
SET current_alert_level=0,
last_update_date=now()AT TIME ZONE 'PST',
next_due_date = (now()+
(SELECT recurrence FROM v_obligation
WHERE obligation_id=4)* '1 day'::interval
WHERE obligation_id = 4
Related
I have a requirement for Redshift SQL where I need to convert a column like '2020-Q2' to start of the quarter date 2020-04-01. I tried few other posts but they don't seem to work. Read somewhere I can use below query but it doesn't really work:
select to_timestamp(to_date('2015-Q1', 'YYYY-MM-DD'), 'HH24:MI:SS');
I am planning to make a UDF but if there is an in-built function already, please share. Thanks!
The following works in Postgres and I think it should work in Redshift:
select to_date(left(yyyyqq, 4), 'YYYY') + (right(yyyyqq, 1)::int - 1) * interval '3 month'
from (select '2015-Q3' as yyyyqq) x
I would like to update multiple rows in the table on the PostgreSQL Database.
I tried to update the date column to the current time by using this SQL command, however, it did not work.
UPDATE data as d SET
date = c.date
FROM (values
('data_id_1', NOW()),
('data_id_2', NOW())
) as c(data_id, date)
where c.data_id = c.date;
I got this error message when I run this SQL command on the console.
ERROR: operator does not exist: text = timestamp with time zone
I am not sure where the is wrong. I would appreciate any advice or suggestions! Thank you very much.
I assume you intend something like this:
update data d
set date = c.date
from (values ('data_id_1', NOW()),
('data_id_2', NOW())
) c(data_id, date)
where c.data_id = d.data_id;
That is, the join condition should be between the tables rather than just on c.
This logic would more typically be written as:
update data d
set date = now()
where d.data_id in ('data_id_1', 'data_id_2')
I can not convert this query from oracle to posgresql. Any help would be appreciated.
Select tdcollid, tddate, tdentry, tdlng, tdlat, tdvpid
From Tracking where Tdcollid = 'jperez'
And Trunc(Tddate) = Trunc(To_Date('14-DEC-16','yyyy-MM-DD'))
order by Tddate
You can do:
Select tdcollid, tddate, tdentry, tdlng, tdlat, tdvpid
From Tracking
where Tdcollid = 'jperez' And
ttdate >= '2016-12-14'::date and
ttdate < '2016-12-14'::date + interval '1 day'
order by Tddate;
Note that the date comparisons are arranged so they can use an index (if appropriate). You can use the same logic as ttdate::date = '2016-12-14'::date if this is not a concern.
Try something like this:
SELECT tdcollid, tddate, tdentry, tdlng, tdlat, tdvpid
FROM tracking
WHERE tdcollid = 'jperez'
AND tddate::date = '2016-12-14'::date
ORDER BY tddate
If tddate is a timestamp, casting it to date with ::date will do the same as Oracle's TRUNC(timestamp). Also date constants should be preferably in ISO-8601 format.
I believe there is some bad data in some of the rows in this table. I'm getting invalid number when I try something like this... other tables don't cause an issue.
Is there someway I can prevent the query from failing?
where to_char(t1.create_date)
between to_char(t2.create_date - INTERVAL '10' MINUTE) and
to_char(t2.create_date + INTERVAL '10' MINUTE)
You could do something like this:
where t1.create_date
between (t2.create_date - 10/1440) and (t2.create_date + 10/1440)
I need some help figuring out and SQL Statement.
I know what I want I just cant express it.
Im using php, so it doesnt need to be exclusivly SQL, its to act as a filter.
Pseudo code
$query="SELECT * FROM MyTable WHERE 'TIS' is not older than 2 days or empty = ''$ORDER"; }
TIS in the name of the column in my table were I store dates in this format 03-12-09 (d,m,y).
The $ORDER is for ordering the values based on values from other fields not dates.
Im looking at
SELECT *
FROM orders
WHERE day_of_order >
(SELECT DATEADD(day,-30, (SELECT MAX(day_of_order) FROM orders)) AS "-30 Days");
But i dont quite think im on the rigth track with this.
Thanks
Try the following:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, SYSDATE) > SYSDATE - INTERVAL '2' DAY
$ORDER
I don't know what database you're using - the above uses Oracle's method of dealing with time intervals. If you're using SQL Server the following should be close:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, GETDATE()) > DATEADD(Day, -2, GETDATE())
$ORDER
In MySQL try this:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, NOW()) > DATE_SUB(NOW(), INTERVAL 2 DAYS)
$ORDER
I hope this helps.
So, I was pretty lost in all this.
How did it got solved:
First I understood that the Statement I was using was not supported by MySql thanks to eligthment from Bob Jarvis.
_ Second In a comment by vincebowdren wich "strongly" adviced me to change the data type on that field to Date wich indeed I had not, it was a string.
It was pretty Dumb for me to try using SQL operations for Dates on a field that had String values.
So I just RTFM: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
and:
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;
Then proceeded to change the field value to date.
and this is my perfectly working query:
$query="SELECT * FROM MyTable WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= TIS OR TIS = 0000-00-00 $ORDER "; }
I would like to thank the posters for their aid.