Reputation: 73
I have a column of dates in my table (referred as org_day). I try to add a new column that represent the day after, that is day_after = org_day + day (or 24 hours) (for all rows of org_day)
From what I've read, the DATE_ADD function of SQL does not work on the entire column, so trying to do something like: DATE_ADD (org_day, INTERVAL 24 HOUR) or DATE_ADD (DATE org_day, INTERVAL 24 HOUR) do not work. The usual examples that do work look like: DATE_ADD (DATE '2019-12-22', INTERVAL 1 day), But I want to perform this operation on the entire column, not on a constant date. Appreciate any help.
Upvotes: 0
Views: 1091
Reputation: 1270493
I would suggest using a view:
create view v_t as
select t.*, date_add(org_day, interval 1 day) as day_after
from t;
If you always want the new column to be in synch with existing column, then a view ensures that the data is consistent. The value is calculated when you query the data.
Upvotes: 1
Reputation: 3663
You can try this:
CREATE OR REPLACE TABLE
mydataset.mytable AS
SELECT
org_day,
DATE_ADD(org_day, INTERVAL 1 day) day_after
FROM
mydataset.mytable;
This above statement will modify the the existing table by adding a new column, without deleting exiting data.
Upvotes: 1
Reputation: 183
To update the entire column, you need to set everything on that column. Try this, hope it solved ur problem...
UPDATE table_name SET column_name = DATE_ADD(var, interval);
Upvotes: 1