How to write pgsql update query with string aggregate? - sql

I have update query that will manually change the field value as a unique string, the table already have a lost of data and the id as unique Pkey.
So I need the names should look like
mayname-id-1,
mayname-id-2,
mayname-id-3, etc
I tried to update with string_agg, but that doesn't work in update queries
UPDATE mytable
SET name = string_agg('mayname-id-', id);
How to construct string dynamically in an update query?

How about the following:
UPDATE mytable
SET name = 'mayname-id-' || CAST(id AS text)

Typically, you should not add such a completely redundant column at all. It's cleaner and cheaper to generate it as functionally dependent value on the fly. You can use a view or a "generated column" for that. Details:
Store common query as column?
You can even have a unique index on such a functional value if needed.

Use string concatenation
UPDATE mytable SET name = 'nayname-id-' || (id :: text);

Related

How to add new key inside json blob in a column in SQL

I have a SQL table MYTABLE similar to the below example
id. config
123. {"location":zxc, 'zip_code':1234}
143. {"location":zxc, 'zip_code':1222}
I need to do a database migration where I add a key name inside the config JSON so the config becomes. {"name": "abc", "location":zxc, 'zip_code':1234}
I am not sure how to do this. I can do ALTER TABLE, ADD COLUMN but this is different than adding/removing a column.
Please suggest.
EDIT:
This is in Postgres
The jsonb_set() function can be used to add properties to the current JSON object in PostgreSql (details). So, for example, you can use the following to set the name property to a value of abc:
UPDATE mytable
SET config = jsonb_set(config, '{name}','"abc"', TRUE)
Where id=1
This can be done using an UPDATE statement:
update mytable
set config = config || '{"name": "abc"}' ;
The above assumes that config is defined as jsonb (which it should be). If it's a json column, you need to cast it: set config = config::jsonb || '{"name": "abc"}'
Since you say it's a migration, I am guessing you are copying rows from an old table to a new table. So, you can modify the value as you are inserting each row. (If you are keeping the same table, change the INSERT statement into an UPDATE statement.) If your JSON is really simple like in your example, then some simple string manipulation should be good enough.
insert NewTable (id, config)
select id,
'{"name": "' + name + '", ' + SUBSTRING(config, 2, DATALENGTH(config) - 1)
from OldTable;
I'm guessing name is another column in this table. If not, modify it to find the name from the right place.

UPDATE column with VARCHAR(2) 4000 byte value

I want to update a table with complex entries in a column and just a clause in the column without effecting other values.
Want to update #Rule number=0# to #Rule number=1# but without affecting other values in the column. Is this possible?
Are you looking for replace()? If dynamic_attributes is a string:
update t
set dynamic_attributes = replace(dynamic_attributes,
'#Rule number=0#',
'#Rule number=1#'
)
where dynamic_attributes like '%#Rule number=0#%';
Note: Strings may not be the best way to store such a list. You should consider a table with one row per customer_id and dynamic attribute.
Here it is:
UPDATE A
SET A.dynamic_attributes = REPLACE(A.dynamic_attributes,'#Rule number=0#','#Rule number=1#')
FROM yourtable AS A
WHERE A.dynamic_attributes LIKE '%#Rule number=0#%'

update a column using values from a different column of the same table

Given the DB table:
CREATE TABLE stuff (
id text not null,
other text
);
That has lots of id values but has all other set to NULL, is there an elegant way to update the table so that all other rows get updated to OTHER-{id} (where {id} is the value of the id column)?
(It must work in Postgresql)
Only a simple update statement is needed with some string concatenation (||):
update stuff
set other = 'OTHER-' || id
You'll want to use the following:
UPDATE stuff
SET other = 'OTHER-' || id;
UPDATE is the keyword used to identify which table you'd like to update.
SET is the keyword used to identify which column you'd like to update, and this is where you choose to assign the column to:
'OTHER-' || id
'OTHER-' being a string
|| a shorthand way to concatenate
id the value you want.
Another way of writing this would be
other = concat('OTHER-',id);
I along with many others will find the || method to be much cleaner, but it's worth knowing about the dedicated function as well.

PostgreSQL - Rule to create a copy of the primaryID table

In my schema I want to have a primaryID and a SearchID. For every SearchID it is the primaryID plus some text at the start. I need this to look like this:
PrimaryID = 1
SearchID = Search1
Since the PrimaryID is set to autoincrement, I was hoping I could use a postgresql rule to do the following (pseudo code)
IF PRIMARYID CHANGES
{
SEARCHID = SEARCH(PRIMARYID)
}
This would hopefully occure exactly after the primaryID is updated and happen automatically. So, is this the best way of achieving this and can anyone provide an example of how it is done?
Thank you
Postgres 11 introduced genuine generated columns. See:
Computed / calculated / virtual / derived columns in PostgreSQL
For older (or any) versions, you could emulate a "virtual generated column" with a special function. Say your table is named tbl and the serial primary key is named tbl_id:
CREATE FUNCTION search_id(t tbl)
RETURNS text STABLE LANGUAGE SQL AS
$$
SELECT 'Search' || $1.tbl_id;
$$;
Then you can:
SELECT t.tbl_id, t.search_id FROM tbl t;
Table-qualification in t.search_id is needed in this case. Since search_id is not found as column of table tbl, Postgres looks for a function that takes tbl as argument next.
Effectively, t.search_id is just a syntax variant of search_id(t), but makes usage rather intuitive.

How to reverse data of a column into another column in MySql?

I need to run a script to update a field of a table in mysql by reversing the string stored in another field of the same table. Original column is term and the one to be filled with the reversed is revTerm. I use this, but it produce an error. Any suggestions?
UPDATE `tu_cla_terms` WHERE tId = '11583' SET revTerm = REVERSE(term)
You have the syntax of an UPDATE query wrong. SET comes before WHERE:
UPDATE `tu_cla_terms` SET revTerm = REVERSE(term) WHERE tId = '11583'
If tId is a numeric column you don't want quotes around its value either.