Querying for a value out of a json field Postgres - sql

I have the following column in my database which is a postres jsonb object. I am just trying to fetch the value of additional_data from mytable which has the json string in it.
select additional_data from mytable;
{"latitude":"123", "longitude":"234"}
{"latitude":"567", "longitude":"890"}
i'm just trying to get the value of latitude for my query and I am unable to figure out how the exact syntax would need to be for this. I tried to do the following
select latitude->>'additional_data' from mytable;
select latitude->'additional_data' from mytable;
select latitude#>>'additional_data' from mytable;
but when I do this, I get the following error
SQL Error [42703]: ERROR: column "latitude" does not exist
Position: 8
but that column definitely exists. Not sure what I am doing wrong.

According to the PostgreSQL documentation, you will need to reference the column on the left side of the operator, and the JSON key on the right side of the operator:
select additional_data->>'latitude' from mytable;
select additional_data->'latitude' from mytable;
select additional_data#>>'latitude' from mytable;

Related

How to get data type column from sql query has casting

How to get data type column from SQL query has casting or custom query, not from view or table
like database client pgAdmin4 can detect type data on column from result query
Like mentioned in the comments, you can you use pg_typeof. Not sure if you added the picture after the comment, but you can also use this for your results as requested in the picture:
select pg_typeof(val1), pg_typeof(val2)
from (
select 123 as val1, 123::text as val2
)z
Results:
|integer|text|

Invalid token error when using jsonb_insert in postgresql

As a little bit of background. I want to fill a column with jsonb values using values from other columns. Initially, I used this query:
UPDATE myTable
SET column_name =
row_to_json(rowset)
FROM (SELECT column1, column2 FROM myTable)rowset
However, this query seems to run for way too long (a few hours before I stopped it) on a dataset with 9 million records. So I looking for a solution with the second FROM clause and found the jsonb_insert function. To test the query I first ran this sample query:
SELECT jsonb_insert('{}','{column1}','500000')
Which gives {'column1':500000} as output. Perfect, so I tried to fill the value using the actual column:
SELECT jsonb_insert('{}':,'{column1}',column1) FROM myTable WHERE id = <test_id>
This gives a syntax error and a suggestion to add argument types, which leads me to the following:
SELECT jsonb_insert('{}':,'{column1}','column1')
FROM myTable WHERE id = <test_id>
SELECT jsonb_insert('{}'::jsonb,'{column1}'::jsonb,column1::numeric(8,0))
FROM myTable WHERE id = <test_id>
Both these queries give invalid input type syntax error, with Token 'column1' is invalid.
I really can not seem to find the correct syntax for these queries using documentation. Does anyone know what the correct syntax would be?
Because jsonb_insert function might need to use jsonb type for the new_value parameter
jsonb_insert(target jsonb, path text[], new_value jsonb [, insert_after boolean])
if we want to get number type of JSON, we can try to cast the column as string type before cast jsonb
if we want to get a string type of JSON, we can try to use concat function with double-quotes sign.
CREATE TABLE myTable (column1 varchar(50),column2 int);
INSERT INTO myTable VALUES('column1',50000);
SELECT jsonb_insert('{}','{column1}',concat('"',column1,'"')::jsonb) as JsonStringType,
jsonb_insert('{}','{column2}',coalesce(column2::TEXT,'null')::jsonb) as JsonNumberType
FROM myTable
sqlfiddle
Note
if our column value might be null we can try to put 'null' for coalesce function as coalesce(column2::TEXT,'null').

psql column doesn't exist but it does

I am trying to select a single column in my data table using raw SQL in a postgresql database from the psql command line. I am getting an error message that says the column does not exist. Then it gives me a hint to use the exact column that I referenced in the select statement. Here is the query:
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
Here is the error message:
ERROR: column insider_app_ownershipdocument.transactiondate does not exist
SELECT insider_app_ownershipdocument.transactionDate FROM in...
HINT: Perhaps you meant to reference the column "insider_app_ownershipdocument.transactionDate".
I have no idea why this is not working.
(Postgres) SQL converts names automatically to lower case although it support case-sensitive names. So
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
will be aquivalent to:
SELECT insider_app_ownershipdocument.transactiondate FROM insider_app_ownershipdocument;
You should protect the column name with double quotes to avoid this effect:
SELECT insider_app_ownershipdocument."transactionDate" FROM insider_app_ownershipdocument;

Select columns from table in schema different than public

In my PostgreSQL database I have table that is inside import schema. When I want to get all data from the column I do:
select * from import.master_plan
This query works fine. But when I try to for example get only title column values:
select import.master_plan.title from import.master_plan;
it returns:
ERROR: column master_plan.title does not exist
LINE 1: select import.master_plan.title from import.master_plan;
^
HINT: Perhaps you meant to reference the column "master_plan.  title".
I've also tried:
select title from import.master_plan;
but this also not works. I'm using PostgreSQL 10. How can I fix that?
I would suggest that you use a table alias instead:
select mp.title
from import.master_plan mp;
This is much easier to read and to type.
Judging from the error message, though, the name seems to have leading spaces. Something like:
select mp." title"
from import.master_plan mp;
might work. If this is the case, alter the table and rename the column.

derby Syntax error: Encountered EOF Next Value sequence

Working with embedded database derby version 10.12.1.1.
I have created a sequence successfully as below
CREATE SEQUENCE BUCKET_SEQ AS BIGINT START WITH 1000;
But when trying to get next value using
SELECT NEXT VALUE FOR BUCKET_SEQ
below error encountered:
Syntax error: Encountered "<EOF>" at line 1, column 40.
Please suggest any pointers.
You have to SELECT from something, and the something has to be some sort of a table.
The simplest thing to do is to use the SQL VALUES keyword, which makes an (unnamed, temporary) table for you.
You then give the table a name, and the table's column a name, and select the value from that:
select t from ( values next value for bucket_seq ) s( t);
T
--------------------
1000
There are other syntax forms possible, but this is a simple one that you can use.