db2 removing generated always on timestamp columns - db2

I have a Column UPDATE_TIME
having the expression
TIMESTAMP NOT NULL GENERATED ALWAYS FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP
how to remove the Generated always for Timestamps
I also tried
db2 "alter table xxxx alter column UPDATE_TIME drop expression"

Since it is defined as a ROW CHANGE TIMESTAMP
You have to drop the column and re-add it.
Why would you want to do this in the first place?

Related

Create new Date column of DATE type from existing Date column of TEXT type in PostgresSQL

I have a PostgresSQL table that has a Date column of type TEXT
Values look like this: 2019-07-19 00:00
I want to either cast this column to type DATE so I can query based on latest values, etc.... or create a new column of type DATE and cast into there (so I have both for the future). I would appreciate any advice on both options!
Hope this isn't a dupe, but I havn't found any answers on SO.
Some context, I will need to add more data later on to the table that only has the TEXT column, which is why i want to keep the original but open to suggestions.
You can alter the column type with the simple command:
alter table my_table alter my_col type date using my_col::date
This seems to be the best solution as maintaining duplicate columns of different types is a potential source of future trouble.
Note that all values in the column have to be null or be recognizable by Postgres as a date, otherwise the conversion will fail.
Test it in db<>fiddle.
However, if you insist on creating a new column, use the update command:
alter table my_table add my_date_col date;
update my_table
set my_date_col = my_col::date;
Db<>fiddle.

Adding generated column existing table in postgresql

I'm trying to add a generated column in existing table. I've a transaction table, in that table I've one column named as dateTime(containing date and time in timestamp format). I want to create a virtual column named as transactionDate which will contain a date and time derived from dateTime column.
Below is the query which I created
ALTER TABLE public.transaction
ADD COLUMN "transactionDate" timestamp without time zone GENERATED ALWAYS AS (timestamp("dateTime")::date) STORED;
and I'm getting below error:
ERROR: syntax error at or near ""dateTime""
LINE 2: ... without time zone GENERATED ALWAYS AS (timestamp("dateTime"...
^
SQL state: 42601
Character: 121
Please help me out.
What are you using timestamp() for?
For generated column, it should be like -
ALTER TABLE transaction ADD COLUMN "transactionDate" timestamp without time zone GENERATED ALWAYS AS ("dateTime"::date) STORED;
OR
ALTER TABLE transaction ADD COLUMN "transactionDate" timestamp without time zone GENERATED ALWAYS AS ("dateTime"::timestamp) STORED;
Fiddle here.
The column dateTime is already a timestamp so you do not need to create a timestamp from it. So just: ( see demo)
alter table transaction
add column "transactionDate" date
generated always as ("dateTime"::date) stored;

How to get redshift to add current time for a field specified in copy command

I have a TSV file that I want to load into redshift via the copy command.
I want one of the fields in the table to be a timestamp that registers the time the row was loaded.
I have defined a field like this:
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
This works fine if I insert into this row at the psql command line, without specifying a value for this column - it defaults to the current timestamp as expected.
However, what can I have in my TSV file in that column that will cause redshift to default to the current timestamp?
If I use \N in my TSV, then I just get a NULL in the ts field.
On the other hand, if I define my column as NOT NULL
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
then I get an error from the COPY command that I can't insert NULL values into a NOT NULL field.
On mysql, mysql would convert a NULL value into the current timestamp, but redshift's behaviour is to throw an error.
Any suggestions? Many thanks!
I've been banging my head over this for a while and found one partial workaround: you can have ts column as the last column of your table and the TSV file with all the other columns but this one. The file will be read with the columns that exist and loaded into the consecutive list of columns with the same width in the target table, leaving all columns beyond that width with default values, i.e. you can have id | ts table and load the file with id only and ts will take the default. The current timestamp column is typically a metadata column, so it's ok to place it at the end of the table.

How to add default value 0000-00-00 to date datatype in postgresql?

I previously changed data type of my column by using below command but now I want to add default value like 0000-00-00 for the same column, can any one help me?
alter table table name alter column name type date using(column name::date)
You have an issue if the column is a date because 0000-00-00 is not a valid date. The syntax for setting the default is:
alter table t alter column col set default '0001-01-01';
However, you need a valid date for that. I would recommend just using NULL if that works for your application.

Importing csv into Postgres database with improper date value

I have a query which has a date field with values that look like this in the query results window:
2013-10-01 00:00:00
However, when I save the results to csv, it gets saved like this:
2013-10-01T00:00:00
This is causing a problem when I'm trying to COPY the csv into a table in Redshift, where it gives me an error stating that the value is not a valid timestamp (the field I'm importing to is a timestamp field).
How can I get it so that it either strips out the time component completely, leaving just the date, or at least that the "T" is removed from the results?
I'm exporting results to csv using Aginity SQL Workbench for Redshift.
According to this knowledgebase article:
After import, add new TIMESTAMP columns and use the CAST() function to
populate them:
ALTER TABLE events ADD COLUMN received_at TIMESTAMP DEFAULT NULL;
UPDATE events SET received_at = CAST(received_at_raw as timestamp);
ALTER TABLE events ADD COLUMN generated_at TIMESTAMP DEFAULT NULL;
UPDATE events SET generated_at = CAST(generated_at_raw as timestamp);
Finally, if you forsee no more imports to this table, the raw VARCHAR
timestamp columns may be removed. If you forsee importing more events
from S3, do not remove these columns. To remove the columns, run:
ALTER TABLE events DROP COLUMN received_at_raw; ALTER TABLE events
DROP COLUMN generated_at_raw;
Hope that helps...