How to append a custom object to JSONB column type in postgres - sql

I need to update a jsonb column which contains data {"X":true}.
I need to append a complex object of the type {"obj":{"a":1,"b":2}} so the final value of row for that column {"x":true,"obj":{"a":1,"b":2}} . What will be the query to update this row .
postgres version 12
Update - The following query update tableName set columnName = (select '{"obj":{"a":1,"b":2}}'::jsonb || columnName::jsonb) where ... returns successfully when there is a value present , but when the column is null it still remains null after running the update query . I need to be able to add {"obj":{"a":1,"b":2}} even when the column is null .

You can use the concatenation operator:
'{"X":true}'::jsonb || '{"obj":{"a":1,"b":2}}'::jsonb
If you want to update an existing column, use coalesce() to deal with NULL values:
update the_table
set the_column = coalesce(the_column, '{}')||'{"obj":{"a":1,"b":2}}'
Online example

Related

There are multiple column in my table and I want to skip one column if value is null for that one particular column in oracle update query?

There are multiple column in my table and I want to skip one column if value is null for that one particular column and all other columns should be updated with there respective value in oracle update query.
Is there any simple way to do this ?
Example:
Let me explain my problem with example:
I am using merge query like below :
merge into table_X on (condition )
WHEN MATCHED THEN
UPDATE SET COLUMN_1=VALUE1 , COLUMN_2=VALUE_2,........
WHEN NOT MATCHED THEN
INSERT ( COLUMN_1,COLUMN_2...) VALUES (VALUE_1,VALUE_2);
Now in update statement I want to skip the update for one column suppose COLUMN_2 if its value is null , but all other column should be updated . Basically I want to preserve the existing value when null is coming .

Update column B based on Column A for all rows in table

I need to insert hash value into column b based on value of column a, but I need to do this for every row in table.
I always get this error no matter what I tried:
ERROR: more than one row returned by a subquery used as an expression
I have been trying different versions of the following:
UPDATE table
SET column b = md5((SELECT column a FROM table))
WHERE column a IS NOT NULL;
Any suggestions on how to perform this operation?
No need for a subquery here. As I understand, you want to store the checksum of column_a in column_b. As one would expect, Postgres' md5() function expects a single, scalar argument of string datatype, so:
UPDATE table
SET column_b = md5(column_a)
WHERE column_a IS NOT NULL;
Note that it would probably be simpler to use a computed column (available in Postgres 12) to store this derived information.

Want to delete specific column in a table

I want to delete specific values/data from one column with the WHERE condition. Putting in another way, I don't want to delete the complete row. Is it possible?
You can re-set values using update:
update t
set col = NULL
where . . .;
Yes it is possible to delete the particular value/data from the column using WHERE condition. You can use the below code in SQL command to delete that particular column.
Update Table_Name set Column_Name=null where Column_Id=xxx
or you can even update that by changing the Column_name value to an empty string ' ' Like below
Update Table_Name set Column_Name='' where Column_Id=xxx

select statement - update the value of one field with a fixed value

I have a select statement that returns a few fields from a table.
I want to update only the results of that select, giving a fixed value in one field.
I though of that, but it doesn't work:
UPDATE
(SELECT * from table.... where...)
SET field1=1
You didn't need a SELECT, just use a WHERE clause directly with the UPDATE to do this only for the rows that statify the condition in the WHERE clause:
UPDATE t
SET field1 = 1
FROM table AS t
WHERE ...
If your are using t-sql
UPDATE
SET field = fixed value
from tablename
where filed....

How can I copy data from one column to another in the same table?

Is it possible to copy data from column A to column B for all records in a table in SQL?
How about this
UPDATE table SET columnB = columnA;
This will update every row.
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
This will update all the rows in that columns if safe mode is not enabled.
UPDATE table SET columnB = columnA;
If safe mode is enabled then you will need to use a where clause.
I use primary key as greater than 0 basically all will be updated
UPDATE table SET columnB = columnA where table.column>0;
If you want to copy a column to another column with a different data type in PostgresSQL, you must cast/convert to the data type first, otherwise it will return
Query 1 ERROR: ERROR: column "test_date" is of type timestamp without
time zone but expression is of type character varying LINE 1: update
table_name set test_date = date_string_col
^ HINT: You will need to rewrite or cast the expression.
An example of converting varchar to timestamp:
update table_name set timestamp_col = date_string_col::TIMESTAMP;
An example of converting varchar to int:
update table_name set int_column = string_col::INTEGER;
but any column type(except file or the similar) can be copied to string(character varying) without cast the type.