How to update multiple rows in PostgresDB with NOW() - sql

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')

Related

Update table based on a time range

Is there a way I could update based on a date range in a postgres DB?
Meaning if I have a row with following values,
name code some_timestamp
abc 1 2020-09-07T13:22:23.206862
I want to update the row base on the name and some_timestamp fields.
I could do the following and it works.
UPDATE myTable SET code=2 WHERE name='apple' and some_timestamp='2020-09-07T13:22:23.206862';
But I do not want to target the specific datetime and instead be able to update as long as the
name matches and date range falls within the same day.
Is there a way to achieve this? Tried via date_trunc. No errors but it doesn't perform the update. (Tried with LIKE too and same outcome).
update myTable set code=1 where name='apple' and some_timestamp=date_trunc('day', TIMESTAMP '2020-09-07T13:22:23.206862');
Otherwise achievable via the BETWEEN key but trying to see if this can be done via date_trunc. Please advice. Thanks.
I would recommend:
update myTable
set code=1
where name = 'apple' and
some_timestamp >= date_trunc('day', TIMESTAMP '2020-09-07T13:22:23.206862') and
some_timestamp < date_trunc('day', TIMESTAMP '2020-09-07T13:22:23.206862') + interval '1 day';
You can also use:
update myTable
set code=1
where name = 'apple' and
date_trunc('day', some_timestamp) = date_trunc('day', TIMESTAMP '2020-09-07T13:22:23.206862');
But this is not as index-friendly.
Use date_trunc() on both sides:
update myTable
set code=1
where name='apple'
and date_trunc('day', some_timestamp) =
date_trunc('day', TIMESTAMP '2020-09-07T13:22:23.206862');

Nested query in INTERVAL

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

SQL Server DateTime and SQL

I am having trouble with the following simple query
SELECT * FROM users WHERE Created = '28/02/2013'
The issue is the column CREATED is a datetime datatype, so the query executes fine as long as the timestamp is 0:00:0, if the time stamp is set to say 12:00, then the query does not return a result set.
Any idea why?
Thanks
Because you are not specifying the time, so it assumes that you are doing:
SELECT * FROM users WHERE Created = '28/02/2013 00:00:00'
If you want the whole day, then you need a range of times:
SELECT *
FROM users
WHERE Created >= '20130228' AND Created < '20130301'
Also, please use non ambiguous format for dates ('YYYYMMDD') instead of other formats.
SELECT * FROM users WHERE CAST(Created AS DATE) = '28/02/2013'
will fix it, but be careful, it disables indexes
SELECT * FROM users WHERE Created BETWEEN '28/02/2013 00:00' AND '28/02/2013 23:59'
And this will use index
If you don't need to consider time: try to convert created field to date and then compare as;
SELECT * FROM users WHERE convert(date,Created) = '28/02/2013'
--this would be even better with iso date format (not culture specific)
SELECT * FROM users WHERE convert(date,Created) = '20130228' --yyyymmdd format
You have to convert the column into date and then compare
SELECT * FROM users WHERE CONVERT(VARCHAR(11), Created, 106) = '28 Feb 2013'

ignoring timestamp in WHERE clause when comparing to date

My table has records like these
23-MAY-11 11.40.39.000000 AM
The following query brings nothing
SELECT *
FROM my_table
WHERE tenant_pha = 'test'
AND create_date >= TO_DATE('05/10/2011','mm/dd/yyyy')
AND create_date <= TO_DATE('05/23/2011','mm/dd/yyyy')
However, the below query will bring data
SELECT *
FROM my_table
WHERE tenant_pha = 'test'
AND create_date >= TO_DATE('05/10/2011','mm/dd/yyyy')
AND create_date <= TO_DATE('05/24/2011','mm/dd/yyyy')
I think this is because create_date column is time stamp.
How can I change my query to bring the desired result ( I want to avoid doing functions on the left side columns because they will make the query long).
You are right about the timestamp. '05/23/2011' is the same as '05/23/2011 12:00 AM'.
To include the whole day I usually move my date up by a day. < '05/24/2011' will include all of 5/23.
or change to '05/23/2011 23:59:59'
You can use trunc() without problems, you only need to create a function based index.
If you create this index:
CREATE INDEX idx_trunc_date ON my_table (trunc(create_date));
then the following condition will make use of that index:
AND trunc(create_date) >= TO_DATE('05/10/2011','mm/dd/yyyy')

SQL date Statement

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.