PostgreSQL refuses to update DATE column - sql

I have a table that contains one value, period_start TIMESTAMP NOT NULL. I'd like to have a second column that stores the day of that timestamp.
I tried the following sql:
ALTER TABLE foo ADD COLUMN day DATE;
UPDATE foo SET day = period_start::DATE;
ALTER TABLE foo ALTER COLUMN day SET NOT NULL;
Now, when I execute the above, I get an error that day contains null values. Looking at the content of the table, I see that every row has NULL as the value of day.
However, SELECT *, period_start::DATE FROM foo gives me the expected result.
Why does my UPDATE statement fail and how can I fix it so that the day column gets correctly populated?

Related

Duplicate a table by changing the name of the duplicated table

I want to duplicate a table by changing the name of the duplicated table by adding today's date.
The date should be generated automatically (the day of the execution of the request)
Example :
Original Table Name = x
Duplicate Table Name = x_20221216 <-- today's date
create table x_20221216 as select * from x

How to add column to database with default

I have a database that I'm trying to add a column to. This column should hold information of the type timestamp, and I want every row to have the same timestamp (the current time) when I'm done.
I currently have tried:
cursor.execute('''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT ?''', (datetime.datetime.utcnow(),))
Which results in sqlite3.OperationalError: near "?": syntax error.
So then I tried:
cursor.execute(f'''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT {datetime.datetime.utcnow()}''')
Which results in sqlite3.OperationalError: near "-": syntax error.
Also, doing
cursor.execute(f'''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT CURRENT_TIMESTAMP''')
results in sqlite3.OperationalError: Cannot add a column with non-constant default.
How can I add the new column and set the values in that column? (Either through DEFAULT, or some other mechanism.)
SQLite does not allow adding a new column with a non-constant value. So this:
alter table my_table add column my_time timestamp default current_timestamp;
... generates error:
Cannot add a column with non-constant default
A simple option would be to recreate the table. Assuming that you have single column called id, that would look like:
create table my_table_new(
id int primary key,
my_time timestamp default current_timestamp
);
insert into my_table_new(id) select id from my_table;
drop table my_table; -- back it up first !
alter table my_table_new rename to my_table;
You can first add the new column and then update every existing row in the table to the desired value:
ALTER TABLE my_table ADD COLUMN time;
UPDATE my_table SET time = CURRENT_TIMESTAMP;

add not null column take values from other column

I have an existing table say, Items with Id | CreateDate (as DateTime) columns; want to add Year int not null, take the default year from the CreateDate column.
The Year value will be futer updated to different value, but just set it as default value when the column Year is added, because want to add non-nullable column to Year.
Is that possible?
If you want to add a new column and initialize it with the values from the other one, you can do:
-- add the column (as nullable)
alter table mytable add created_year int;
-- update the values on existing rows
update items set created_year = year(created_date);
-- make the column not nullable
alter table mytable alter column created_year int not null;
Don't bother with another column -- unless you will feel the need to change the value of the column in the future. Instead, just use a computed column:
alter table t add created_year as (year(created_date));
This is automatically calculated when you query the table, so it is always accurate, both for new rows and old rows.
Presumably, created_date is never updated.

Add a column with the current timestamp to a table in Hive

I'm trying to add a column called tstamp to a table that I've created. The column is to have the current timestamp in the format 'yyyy-MM-dd' populating each row.
I initially created the table from another table (table1) using the statement:
create location2.table2
as (select *
from location1.table1
);
I then used the alter table statement to add a field called tstamp to table2 using the code:
alter table location2.table2
add columns (tstamp date)
and I can see that this has successfully added a column to table2 named tstamp and populated each row of this table as null. I am now trying to insert the current date into every row in the field tstamp but am struggling to do so. I've tried using the insert into statement as:
insert into location2.table2 (tstamp)
values (to_date(current_timestamp()))
but get the error "Expression of type TOK_FUNCTION not supported in insert/values". I then also tried to add just a string and replaced the function with '2019-07-25'. Doing this added a new row to my table with null values in every column except tstamp which had a value '2019-07-25'. I'm now confused as it appears my approach was not the right one for the problem and am unsure where to go from here. Any help would be greatly appreciated.
create location2.table2 as (select current_date as tstamp,* from location1.table1 );

How to add null values for all columns for missing dates and time in SQL

I have a table with user name, dates, time of day and value associated for every hour.
I want to add missing dates for each user for all times of the day with null valued for values.
when u created the set it null
create table tablename
(
colname as datetime null
)
this will set bydefault its value to null