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;
Related
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.
I am trying to add a default timetable into my query with a chosen time, 09.00 o'clock. The type of the column was 'date', I changed it to 'timestamp', my assignment sais so. I don't know what to do now. I am very new to this and trying to understand queries with 'timestamp'.
This is what I have so far:
ALTER TABLE note
ALTER COLUMN entered TYPE timestamp SET DEFAULT;
I don't know what to do next. Any help is appricieted!
The ALTER COLUMN allows to provide an expression for the cast through the USING keyword. As you already have a date, you can convert it to a timestamp by adding a time:
ALTER TABLE note
ALTER COLUMN entered TYPE timestamp using entered + time '09:00';
If you also want to set a default value to "today at 09:00" you can do that in the same statement:
ALTER TABLE note
ALTER COLUMN entered TYPE TIMESTAMP USING entered + time '09:00',
ALTER COLUMN entered SET DEFAULT current_date + time '09:00';
After research on here I wanted to use "timestamp with time zone" but cannot figure out the proper syntax based on the postgres docs.
ALTER TABLE microwaves ADD COLUMN scanned_in DATA_TYPE timestamp with time zone;
ALTER TABLE microwaves ADD COLUMN scanned_in TYPE timestamp with time zone;
Both throw errors.
Any help appreciated, thanks.
You just had the syntax wrong. You don't need the [DATA] TYPE part here (that's only needed when you want to change the type) :
CREATE TABLE barf
( id serial PRIMARY KEY);
ALTER TABLE barf ADD COLUMN scanned_in timestamp with time zone;
BTW (just a hint): most of the ALTER syntax just mimics the syntax for CREATE TABLE (...): the sub-syntax is mostly the same.
Follow the simple solution as below:
ALTER TABLE microwaves ADD COLUMN scanned_in timestamp with time zone;
For more details check PostgreSQL - ADD COLUMN
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?
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...