Generated column from date column in postgresql 10 - postgresql

Having a problem with this, trying to add a column which returns the year from a date column in postgresql 10: This is my SQL
ALTER TABLE stats ADD COLUMN year_ integer GENERATED ALWAYS AS (date_part('year', date)) STORED;
However, this returns a syntax error in the SQL window. I have looked up the online documentation and have no idea what is going wrong, also tried with the EXTRACT function and I keep on getting the same error of the sort
ERROR: syntax error at or near "("
LINE 1: ...tats ADD COLUMN year_ integer GENERATED ALWAYS AS (date_part...

Related

Postgres error when parsing date with timezone

I have a table with a bunch of records, with different values for a date and I need them all parsed as a date value, so I'm trying to parse a date in postgres and I'm receiving an error which doesn't tell me much
select to_Date(:original_date, 'YYYYmmDD');
When I pass this value to original_date is when I get the error: '2022-11-18T11:02:08-03:00'
Here's the error I'm getting:
SQL Error [22008]: ERROR: date/time field value out of range: "2022-11-18T11:02:08-03:00"
Where: SQL statement "select to_Date(original_date, 'YYYYmmDD')"
PL/pgSQL function parse_date(character varying) line 5 at SQL statement
As mentioned by Hambone in the comment below the question, changing my date format to 'YYYY-mm-DD' works like a charm.
Thanks for that Hambone!

creating pg_partman parent table generates error `invalid input syntax for type bigint: "monthly"`

I'm trying to use pg_partman extension for the first time but cannot create the parent table. This is my command:
SELECT partman.create_parent('public.mytable_by_date','start_time','native','monthly');
That generates this error:
ERROR: invalid input syntax for type bigint: "monthly"
CONTEXT: PL/pgSQL function partman.create_parent(text,text,text,text,text[],integer,text,text,boolean,text,text,text[],boolean,text,boolean,text) line 551 at assignment
DETAIL:
HINT:
CONTEXT: PL/pgSQL function partman.create_parent(text,text,text,text,text[],integer,text,text,boolean,text,text,text[],boolean,text,boolean,text) line 788 at RAISE
I tried daily and quarterly too but they return same error.
pg_partman: 4.6.0
Postgres:12.9
Ubuntu: 20.04.01
I found out the Problem.
The start_time column type is integer and data is stored using unix timestamp but as number and monthly only works with data columns.
I have to convert start_time column to timestamp first.

Troubled trying to convert a column data type in pgAdmin from varchar to integer (SQL state: 22P02)

I am simply trying to convert a column with character varying data type to integer by run this bit of Postgres script:
ALTER TABLE tbl.test ALTER COLUMN "XX" TYPE integer USING ("XX"::integer);
and I get this error:
ERROR: invalid input syntax for integer: "XX" SQL state: 22P02
Can someone help me to resolve this issue, please?
Based on the error you get it looks like you have some values in XX that cannot be converted to an integer. You will need to correct those values before issuing the alter table.
Find your incorrect values with this query:
select "XX" from tbl.test where "XX" !~ '^-{0,1}\d+$';
The !~ is the NOT regex match. The regex anchors to the beginning of the value with ^, accounts for an optional minus sign with -{0,1}, which matches zero or one hyphen character, and then insures that the remaining characters to the end of the value are all digits with \d+$.
Any values of XX that fail to match this pattern will be retrieved, and you can figure out how to deal with them either by updating the table or modifying the using part of your alter table.

Laravel Eloqeunt CAST() table column with postgres generating error

I am using Laravel 5.2 with Postgres. I have an integer column where I need to put a condition with like %%. I have a postal_code columns where integers values are saved. Now I need to get all rows those have 12 in that. But we cannot do that with integer datatype so I am casting it as text while doing query but I am getting error.
Here is my code
$query->where("cast(postal_code as TEXT)", "LIKE", "%".$request['postal_code']. "%");
And it is generating error
"cast(postal_code" as "text)"
Please see the unwanted " being put in query. This is the query output error. Is this the error Or there is some thing other went wrong and how can I fix that.
I think casting a field on where in laravel doesn't seem to be worked.Execute a raw query using whereRaw function.
$query->WhereRaw("cast(postal_code as TEXT) ILIKE '%?%',[$request['postal_code']]");

currval Function in PostgreSQL complaining that "column does not exist"

I am trying to use PostgreSQL's currval function to return the last inserted row id of a table called Concept. Concept has a serial primary key called cid and there was an automatically generated Sequence called Concept_cid_seq.
I try the following statement and get an error:
SELECT currval("Concept_cid_seq");
ERROR: column "Concept_cid_seq" does not exist
LINE 1: SELECT currval("Concept_cid_seq");
^
********** Error **********
ERROR: column "Concept_cid_seq" does not exist
SQL state: 42703
Character: 16
But when I run the query :
SELECT * from "Concept_cid_seq";
I get a table with one row (as I'd expect) showing columns like last_value, start_value, etc...
What am I missing here? Am I passing the wrong information to currval? Why does it say the 'column does not exist?'
It turns out that this was an issue with capitalization and quotes. Because I wanted to preserve the capitalization of the relation name I needed to use both single and double quotes in order to pass the correct relation name to currval.
I changed the query to SELECT currval('"Concept_cid_seq"'); (note the outer single quotes) and it worked correctly.