UPDATE column with VARCHAR(2) 4000 byte value - sql

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#%'

Related

Adding or removing columns in result set of sql query depending on value of other field

Assume I have a query that returns a result set of columns A and B from table First_Table. I want to limit the result set to those columns if the value of column X in table Second_Table is 0, and I want to add column C from table First_Table if the value of column X is 1.
The problem is easily resolved using a Python for example whereby I just have a variable as an empty string if value in column X is 0 or it would be equal to the string 'First_Table.ColumnC AS [Dynamic Value],', and I just format the sql in the script accordingly.
If Else solution is not an elegant way because I have multiple columns to add dynamically depending on multiple values...
I am just looking for some ideas on directions.. I have been looking at this for a while, might be bogged up
Dynamic sql is the best way to resolve this as suggested in the comments.

Sql Query to update data by prepending text

I have a table with following data :-
I want to change data of SetType Column as 'CATEGORY_3'. So the number will remain as it is, i need to prepend the number with text 'CATEGORY_'.
so SetType values will be like following :-
CATEGORY_3
CATEGORY_3
CATEGORY_5
CATEGORY_5
How to update data by prepeding predefined text by SQL Query?
You use update. Here is one method:
update t
set SetType = replace('Category_#val', '#val', SetType);
This assumes that SetType is a character field. If not, you will need to alter the type of the column before making such a change.
you can use CONCAT.
update table_name set SetType = CONCAT('CATEGORY_', SetType)
CONCAT will simply append strings to one another and you can use multiple strings in argument by separating it with , .
Use UPDATE statement, But if your SetType columns Data Type is Int then first convert it into varchar.
UPDATE your_tablename
SET SetType = 'CATEGORY_'+ CONVERT(NVarchar(2),SetType)
Use UPDATE statement of all rows :
UPDATE your_tablename SET SetType = 'CATEGORY_'+SetType
IF SetType datatype is not in VARCHAR means use below query :
UPDATE your_tablename SET SetType = 'CATEGORY_'+CAST(SetType AS VARCHAR)
If SetType is currently an integer type, it is probably an int for a reason. If you only need certain queries to prepend 'Category_' modify your select queries instead of the updating the table.
select
SetName
, SetType = convert(varchar(20),('Category_'+convert(varchar(11),SetType)))
, FieldValue1
from t

How to write pgsql update query with string aggregate?

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);

Update A multi-valued field in Access

I have created a lookup table in Access to provide the possible values for a column. Now I need to update this column with the data it had before I converted the column. I am unable to figure out a SQL Query that will work. I keep getting the error "An UPDATE or DELETE query cannot contain a multi-valued field." My research has suggested that I just need to set the value of the column but this always updates 0 records:
UPDATE [table_name] SET [column_name].Value = 55 WHERE [table_name].ID = 16;
I know this query will work if I change it to update a text column, so it is definitely a problem with just this column.
If you're adding a value to your multi-valued field, use an append query.
INSERT INTO table_name( [column_name].Value )
VALUES (55)
WHERE ID = 16;
If you want to change one particular value which exists in your multi-valued field, use an UPDATE statement. For example, to change the 55 to 56 ...
UPDATE [table_name]
SET [column_name].Value = 56
WHERE [column_name].Value = 55 And ID = 16;
See Using multivalued fields in queries for more information.
I have figured this out! It certainly was counter-intuitive! You have to use an INSERT statement to do the update.
-- Update a record with a multi-valued field that has no value
INSERT INTO [table_name] ( [[column_name].[Value] )
VALUES(55)
WHERE [table_name].ID = 16;
This confused me because I was expecting an UPDATE statement. I think it actually inserts a record into a hidden table that is used to associate multiple values with this column.
I am working with Sharepoint, I created the tables as multi-value fields, ran into the error with my INSERT INTO statement, went back to Sharepoint to change to non-multi-value fields, but that didn't fix it.
Recreated the table without using multi-value fields, and the INSERT INTO worked just fine.
do not use the .value part
UPDATE [table_name] SET [column_name] = 55 WHERE [table_name].ID = 16;
INSERT INTO Quals (cTypes.[value])
SELECT Quals_ContractTypes.ContractType
FROM Quals_ContractTypes
WHERE (Quals.ID = Quals_ContractTypes.ID_Quals);
I gotta say I didn't understand very well your problem but I saw something strange in your query. Try this:
UPDATE [table_name] SET [column_name]= 55 WHERE [table_name].ID = 16;
UPDATE:
Look at this link: it has an example
UPDATE Issues
SET Issues.AssignedTo.Value = 10
WHERE (((Issues.AssignedTo.Value)=6)
AND ((Issues.ID)=8));
NOTES
You should always include a WHERE
clause that identifies only the
records that you want to update.
Otherwise, you will update records
that you did not intend to change. An
Update query that does not contain a
WHERE clause changes every row in the
table. You can specify one value to
change.
The Multi-Valued field refers to Access databases that have tables with columns, that allow you to select multiple values, like a Combo Checkbox list.
THOSE are the only Access types that SQL cannot work with. I've tested all Access lookup possibilities, including hard-coded values, and lookup tables. They work fine, but if you have a column that has the Allow Multiple select options, you're out of luck. Even using the INSERT INTO as mentioned below, will not work as you'll get a similar but different error, about INSERTing into multi-valued fields.
As mentioned it's best to avoid using such tables outside of Access, and refer to a table specifically for your external needs. Then write a macro/vba script to update the real tables with the data from the "auxiliary" table.

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.