I have a column text_ts which is a text field that always holds data representing a timestamptz, just as a text field. I need to create an index on it such as:
CREATE INDEX IF NOT EXISTS text_ts_timestamp_idx ON my_schema.my_table ((text_ts::timestamptz AT TIME ZONE 'UTC'));
However I get the error:
functions in index expression must be marked IMMUTABLE
I have found this: https://dba.stackexchange.com/questions/250627/why-isnt-it-possible-to-cast-to-a-timestamp-in-an-index-in-postgresql which seems to have a recommended way to handle timestamp, but I can't seem to get it to work for timestamptz AT TIME ZONE 'UTC'.
Anyone have any ideas?
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'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;
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
Change data type varchar to timestamp along with null values in PostgreSQL
I have a column with empty rows and few with timestamp rows. How to convert that into timestamp data type in PostgreSQL?
You need a USING clause to turn the empty strings into null.
ALTER TABLE ...
ALTER COLUMN mycol
TYPE timestamp
USING (...conversion expression...)
Without seeing the input data it's hard to say exactly what that expression must be, but it probably involves nullif or case expressions and the to_timestamp function and/or a CAST to timestamp.