I tried to create generated column from other JSON type column with
ALTER TABLE "Invoice" ADD COLUMN created Integer GENERATED ALWAYS AS (data ->> 'created') STORED;
When I execute this I get error
ERROR: column "created" is of type integer but default expression is of type text HINT: You will need to rewrite or cast the expression. SQL state: 42804
I tried to cast it with CAST function and :: operator but with no lack. Is there any way to do it? Or maybe I should generate this column differently?
Thanks
How about converting the value to an int?
ALTER TABLE "Invoice" ADD COLUMN created Integer
GENERATED ALWAYS AS ( (data ->> 'created')::int ) STORED;
Related
I need to update a version column in a table from VARCHAR to int.
The version column is currently saving versions as a combination of a "v" and a number (e.g. v1, v2, v3), and I would like to have it just be the number.
I tried to use this sql query to update the table column:
alter table vanities alter column version TYPE INTEGER USING(version::integer);
However, I got the following error:
[22P02] ERROR: invalid input syntax for integer: "v1"
Is there a SQL query I could use that would strip the column of all of the v characters and then convert it into an integer?
Try getting rid of the first character:
alter table vanities alter column version TYPE INTEGER USING (substr(version, 2)::integer);
Here is a db<>fiddle.
I have a table entries with a field details which type is jsonb and default value is '{}'::jsonb, on PostgreSQL 10.5
When I run
SELECT details->foo FROM entries
I get an
ERROR: column "foo" does not exist.
From https://www.postgresql.org/docs/10/functions-json.html I understood that I should get a NULL value when the key is not present is the JSON. Did I understand wrongly ? If so, how can I extract the field with a default value ?
You need to supply the JSON key as a string constant:
SELECT details -> 'foo'
FROM entries;
I am using PostgreSQL to create a table based on json input given to my Java code, and I need validations on JSON keys that is passed on the database just like oracle but problem here is the whole jsonb datatype column name lets say data is single column. Consider I get json in below format -
{
"CountActual": 1234,
"CountActualCharacters": "thisreallyworks!"
"Date": 09-11-2001
}
Correct datatype of above json:- number(10), varchar(50), date
Now to put validations on I'm using constraints
Query 1 -
ALTER TABLE public."Detail"
ADD CONSTRAINT "CountActual"
CHECK ((data ->> 'CountActual')::bigint >=0 AND length(data ->> 'CountActual') <= 10);
--Working fine.
But for Query 2-
ALTER TABLE public."Detail"
ADD CONSTRAINT "CountActualCharacters"
CHECK ((data ->> 'CountActualCharacters')::varchar >=0 AND length(data ->> 'CountActualCharacters') <= 50);
I'm getting below error -
[ERROR: operator does not exist: character varying >= integer
HINT: No operator matches the given name and argument type(s).
You might need to add explicit type casts.]
I tried another way also like -
ALTER TABLE public."Detail"
ADD CONSTRAINT CountActualCharacters CHECK (length(data ->> 'CountActualCharacters'::VARCHAR)<=50)
Above constraints works successfully but I don't think this is the right way as my validation is not working when inserting the data -
Insert into public."Detail" values ('{"
CountActual":1234,
"CountActualCharacters":789
"Date": 11-11-2009
}');
And its shows insert successfully when passing in 789 in CountActualCharacters instead of varchar like "the78isgood!".
So please can anyone suggest me proper constraint for PostgreSQL for varchar just like number that I have written in Query 1.
And if possible for Date type also with DD-MM-YYYY format.
I just started with PostgresSQL, forgive me if I'm sounded silly but I'm really stuck here.
You can use jsonb_typeof(data -> 'CountActualCharacters') = 'string'
Note the single arrow, as ->> will try to convert anything to string.
You can read more about JSON functions in PostgreSQL here:
https://www.postgresql.org/docs/current/static/functions-json.html
I have a database with a lot of tables which contain many columns of type boolean. But my software doesn't support boolean as a datatype. Their support team told me that I should use numeric(1,0) instead of boolean, so I tried:
ALTER TABLE foo ALTER COLUMN bar TYPE numeric(1,0) USING bar::numeric(1,0)
I get an error: can't convert boolean into type numeric.
What can I do to replace the boolean columns with a numeric alternative and preserve the boolean behaviour? Can this numeric(1,0) thing even work?
you need an extra step - cast to int first:
t=# select true::int::numeric(1,0);
numeric
---------
1
(1 row)
so in your case it would be:
ALTER TABLE foo ALTER COLUMN bar TYPE numeric(1,0) USING bar::int::numeric(1,0)
but frankly saying I believe just converting to int is already enough.
Here, I am only going to get CheckListNo (integer) from select statement "Check_No" ( character varying). when am execute this, error showing like this 'operator does not exist: character varying = integer'. So, I want to check without changing datatypes.
For an example, SELECT "Check_No"
FROM "Project_CheckList_Options" let we see I defined "check_No" column as a integer if I want to get records in character varying. I can use just "check_No":: character varying then I can see column records in character varying type without affecting integer datatype. So like that Is it possible to convert datatype when alter the table with check constraint in postgresql?
alter table "Project_Configuration"
add check("CheckListNo" in (SELECT "Check_No" FROM "Project_CheckList_Options"))