I have this SQL query and I wanted to insert the value of my bbcode_uid column into the replace statement
UPDATE `phpbb_posts`
SET `post_text` = REPLACE(post_text, '"]', '":*MY_BBCODE_UID*]')
is this possible?
The solution that I used
UPDATE `phpbb_posts`
SET `post_text` = REPLACE(post_text, '"]', CONCAT('":', bbcode_uid, ']'))
if there's better syntax, i'm interested
Related
I created a SET sql table, so a row can contain multiple values: Foo_1 Foo_2 Foo_3.
The above query run but it will update the the row and set just one value.
QueryType valid values are 1 2 or 3
"UPDATE Foo_status
SET type = 'Foo_%s',
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
When i run the query: Let's assume that QueryType is 1, it's working.. my type row will have Foo_1, but when i run again the query and Foo has QueryType 2, it will remove Foo_1 and add Foo_2
How i can change my query so when i run the query it will continue to update values ?
Example:
Run Query
-Add Foo_1
Run Query
-Add Foo_2
Table struct
CREATE TABLE `Foo_status` (
`name` text NOT NULL,
`type` set('Foo_1','Foo_2','Foo_3') CHARACTER SET latin2 NOT NULL DEFAULT '',
`status` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I tried to search for an solution but without succes, that's i asked here.
Thanks!
Multiple set elements are specified using a comma-separated string. To add a new value, you have to concatenate a string beginning with comma.
"UPDATE Foo_status
SET type = CONCAT(type, ',Foo_%s'),
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
To remove a value from a SET, use REPLACE():
"UPDATE Foo_status
SET type = REPLACE(type, 'Foo_%s', ''),
status = %d
WHERE name ='%s'
", queryType.c_str(),status,GetName()
It would be easier if you normalized your schema and used a table with a separate row for each type, instead of using the SET datatype. This would also allow you to add an index that would make searching for specific set values more efficient.
You can remove a set wirh this. Here written for select.
You can it also use for UPDATE.
You only must change the string to remove
select t,
trim( BOTH ',' FROM
replace (concat (',',t,','),',Foo_2,',',')) as new_t
from F;
Sample: http://www.sqlfiddle.com/#!9/a8b041/2
I have a binary column in my table in mysql. I need to update all the rows for example.
Replace 2nd byte with 1. Doesn't matter what the value was.
SELECT HEX(data) FROM table;
Then
UPDATE table SET data[1] = 1; // the idea
But how do you do something like this in mysql?
Here how you can do it. First get the binary representation with hex function.
Then on the returned value use substring and concat function to replace the 2nd value with 1 and finally update.
Here is an example.
CREATE TABLE t (c BINARY(10));
INSERT INTO t SET c = 'b';
select (HEX(c)) from t;
62000000000000000000
SELECT concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
from t
61000000000000000000
Now putting all together in the update command will replace the value
update t set c = unhex(
concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
);
select (HEX(c)) from t;
61000000000000000000
How Can I update strings which is containing 1 in my column.
#Select column from table
sample column: 100001, 100002, 100003, 100004, 100005, 100006, 100007, 100008
#How to update one at a time query?
update column: E00001, E00002, E00003, E00004, E00005, E00006, E00007, E00008
If your values are not stored as comma separated the you can use substring() to get the string after 1,and left() function in where clause to check the value should have 1 as starting character
update t
set `column` = CONCAT('E',SUBSTRING(`column`,2))
where left(`column` , 1) ='1'
Demo
Edit from comments
update t
set `column` = CONCAT(left(`column` , 1),'E',SUBSTRING(`column`,3))
where right(left(`column` , 2),1) ='1'
Demo 2
Try this
UPDATE table
SET `column` = CONCAT(
REPLACE(
LEFT(`column`,1), '1', 'E'),
SUBSTRING(`column`, 2));
I have a column A in a table called T. The column is of type nvarchar(255).
There is a trigger on this table that updates the values in column A to a column B in table B. The column B is of type float.
Suppose if there is value 35.1 % in column A, I want to strip off the % and update only 35.1 in column B. It is not necessary that there should be a % symbol for the value in column A.
How do I do this in SQL Server 2008? Any ideas and suggestions are much appreciated!
You may try to use the following query:
update B
set B.colB = cast(REPLACE(A.colA, '%', '') as float)
from A
where B.idB = A.idA
select replace('35.3 %','%','') or select replace('35.3 ','%','') will both return 35.3; just change your query accordingly; something like:
update tableb
set b=replace(valuefromcola,'%','')
where condition
If you want to get only numbers in a string try this function:
CREATE Function [dbo].[GetNumbersFromString](#Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
While PatIndex('%[^0-9]%', #Temp) > 0
Set #Temp = Stuff(#Temp, PatIndex('%[^0-9]%', #Temp), 1, '')
Return #TEmp
End
You should put this inside your trigger. Please note that you'll need to modify the function to accept the . and , characters (due to those being valid in numbers) =-)
--trim trailing non-alphanumeric chars
DECLARE #input nvarchar(4000)
SET #input = ',,, a word $$$'
SELECT REVERSE(SUBSTRING(REVERSE(#input), PATINDEX('%[a-zA-Z0-9]%', REVERSE(#input)), LEN(#input)))
update B
set B.colB = cast(REPLACE(A.colA, '%', '') as float)
from A
where B.idB = A.idA
We have a database that has a bunch of records with some bad data in one column, in which an embedded editor escaped some stuff that shouldn't have been escaped and it's breaking generated links.
I want to run a query to replace the bad characters in all the records, but can't figure out how to do it. I found the replace() function in MySQL, but how can I use it inside a query?
For example, what would be the correct syntax if I wanted to replace the string < with an actual less-than angle bracket (<) in all records that have < in the articleItem column? Can it be done in a single query (i.e. select and replace all in one swoop), or do I have to do multiple queries? Even if it's multiple queries, how do I use replace() to do the replace on the value of a field on more than one record?
At a very generic level
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%'
In your case you say these were escaped but since you don't specify how they were escaped, let's say they were escaped to GREATERTHAN
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'GREATERTHAN', '>')
WHERE articleItem LIKE '%GREATERTHAN%'
Since your query is actually going to be working inside the string, your WHERE clause doing its pattern matching is unlikely to improve any performance - it is actually going to generate more work for the server. Unless you have another WHERE clause member that is going to make this query perform better, you can simply do an update like this:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'GREATERTHAN', '>')
You can also nest multiple REPLACE calls
UPDATE MyTable
SET StringColumn = REPLACE (REPLACE (StringColumn, 'GREATERTHAN', '>'), 'LESSTHAN', '<')
You can also do this when you select the data (as opposed to when you save it).
So instead of :
SELECT MyURLString From MyTable
You could do
SELECT REPLACE (MyURLString, 'GREATERTHAN', '>') as MyURLString From MyTable
UPDATE some_table SET some_field = REPLACE(some_field, '<', '<')
Check this
UPDATE some_table SET some_field = REPLACE("Column Name/String", 'Search String', 'Replace String')
Eg with sample string:
UPDATE some_table SET some_field = REPLACE("this is test string", 'test', 'sample')
EG with Column/Field Name:
UPDATE some_table SET some_field = REPLACE(columnName, 'test', 'sample')
you can write a stored procedure like this:
CREATE PROCEDURE sanitize_TABLE()
BEGIN
#replace space with underscore
UPDATE Table SET FieldName = REPLACE(FieldName," ","_") WHERE FieldName is not NULL;
#delete dot
UPDATE Table SET FieldName = REPLACE(FieldName,".","") WHERE FieldName is not NULL;
#delete (
UPDATE Table SET FieldName = REPLACE(FieldName,"(","") WHERE FieldName is not NULL;
#delete )
UPDATE Table SET FieldName = REPLACE(FieldName,")","") WHERE FieldName is not NULL;
#raplace or delete any char you want
#..........................
END
In this way you have modularized control over table.
You can also generalize stored procedure making it, parametric with table to sanitoze input parameter
This will help you.
UPDATE play_school_data SET title= REPLACE(title, "'", "'") WHERE title = "Elmer's Parade";
Result:
title = Elmer's Parade