Replace multiple characters using MySQL - mysql

i am using this piece of code to replace characters in one column of my database.
UPDATE items
SET items = REPLACE(items, 'ḇ','ḇ')
But now I have a list with almost 500 characters to replace.
Just writing the whole sequence of lines in one single query will not work.
UPDATE items
SET items = REPLACE(items, 'ḇ','ḇ')
SET items = REPLACE(items, '&#x1E0x;','x')
SET items = REPLACE(items, '&#x1E0y;','y')
ETC.
Or I do not know how to write it.
Can anyone help me please?

Create a table that has the search string and the replace string as columns. Add all 500 rows of what needs to be replaced. Then write a stored procedure that will lookup the replace value from the lookup table and replace with the value. The lookup table can easily be loaded into MySql from an Excel or csv file.
Here's the pseudo code to show the looping and lookup. I know it won't compile, I'm a bit rusty on MySql syntax. I usually work in Oracle so the pseudocode syntax is more Oracle-esque.
DECLARE
v_old_string varchar;
v_new_string varchar;
BEGIN
FOR v IN (SELECT * FROM items) LOOP
SELECT old_string, new_string
INTO v_old_string, v_new_string
FROM my_lookup_table
WHERE old_string = v.thestringcolumn;
UPDATE items
SET itemcolumn = REPLACE(itemcolumn, v_old_string, v_new_string)
WHERE itemcolumn = v_old_string;
END LOOP;
END;

Related

Removing addresses from Column

I have a column within my database that holds text similar to this
CNEWS # Trinidad : "By Any Means Necessary" Watson Duke Swims And Sails To Toco http://somewebsitehere.com
What can I do to remove the entire http address from the column? Please note that some links may be broken so it may have http:// somewebsitehere.com
I was thinking of using a substring index but not sure that would work.
You could use whichever your favorite programming language is to iterate through the rows in the table, pluck out that column, apply a regular expression replacement rule to it, then update the row in the table with the new value.
Here is some pseudo-code:
theRows = SELECT * FROM TheTable WHERE 1;
foreach row in theRows
BEGIN
oldColumnValue = row[theColumnName]
// Removes any link appearing at the end of the column
newColumnValue = oldColumnValue.replace(/http:\/\/[^\s]*$/, '')
UPDATE TheTable SET theColumnName = newColumnValue WHERE id = row[id]
END
For something as small and specific as this, you could use perl with the DBI library to connect to mySQL. Here's a useful resource on regular expressions if you want to go more into it: http://www.regular-expressions.info/perl.html

MySQL Add Column that Summarizes data from Another Column

I have a column in MySQL table which has 'messy' data stored as text like this:
**SIZE**
2
2-5
6-25
2-10
26-100
48
50
I want to create a new column "RevTextSize" that rewrites the data in this column to a pre-defined range of values.
If Size=2, then "RevTextSize"= "1-5"
If Size=2-5, then "RevTextSize"= "1-5"
If Size=6-25, then "RevTextSize"="6-25"
...
This is easy to do in Excel, SPSS and other such tools, but how can I do it in the MySQL table?
You can add a column like this:
ALTER TABLE messy_data ADD revtextsize VARCHAR(30);
To populate the column:
UPDATE messy_data
SET revtextsize
= CASE
WHEN size = '2' THEN '1-5'
WHEN size = '2-5' THEN '1-5'
WHEN size = '6-25' THEN '6-25'
ELSE size
END
This is a brute-force approach, identifying each distinct value of size and specifying a replacement.
You could use another SQL statement to help you build the CASE expression
SELECT CONCAT(' WHEN size = ''',d.size,''' THEN ''',d.size,'''') AS stmt
FROM messy_data d
GROUP BY d.size
Save the result from that into your favorite SQL text editor, and hack away at the replacement values. That would speed up the creation of the CASE expression for the statement you need to run to set the revtextsize column (the first statement).
If you want to build something "smarter", that dynamically evaluates the contents of size and makes an intelligent choice, that would be more involved. If was going to do that, I'd do it in the second statement, generating the CASE expression. I'd prefer to review that, befor I run the update statement. I prefer to have the update statement doing something that's easy to understand and easy to explain what it's doing.
Use InStr() to locate "-" in your string and use SUBSTRING(str, pos, len) to get start & End number. Then Use Between clause to build your Case clause.
Hope this will help in building your solution.
Thanks

I need to concat two numbers in mysql

set n=901234567;
set p=0;
insert into try values (
concat(fname,i),
concat(sname,i),
concat(age,i),
concat(email,i,id),
concat(n,p)
);
set n=n+1;
set i=i+1;
concat is working for first four columns.but its not working for concating n and p.here am incrementing the value of n .And for every incrementation the value in p i.e 0 sholud be added at the end of the value of n.
What are n and p? System variables? Most probably not. There is no need to search them into the list of MySQL server system variables, their names are too short, there is no chance to find them there.
Oh, wait! They are user-defined variables. Or, at least, this is what you want them to be. Except that the user-defined MySQL variables are always prefixed with #.
Back to the code, n and p are not user-defined variables and the user is not allowed to create system variables. MySQL returns the error Unknown system variable 'n' on the first SET query. Case closed.
Let's try to make it work:
set #n=901234567;
set #p=0;
insert into try values (
concat(fname,#i),
concat(sname,#i),
concat(age,#i),
concat(email,#i,id),
concat(#n,#p)
);
set #n=#n+1;
set #i=#i+1;
If the other names present in the queries (fname, sname, age, email, id) are also variables then put # in front of them.

Use CHARINDEX rather than IN clause to speed up SSRS report filters

In some cases, we are using IN clause in our filters of SSRS reports. A lots of them are causing issues with the performance by using hundreds of items inside of IN clauses.
such as:
WHERE TableA.School IN (#School)
sometimes, the multi-value parameters are really tricky to handle, you might need to do =Join(Mypara.Value,",") in the RDL and write a SQL function to convert them into a set of SQL data to be able to feed the SQL SP. (especially some older version of SSRS).
FYI: function to use to break a comma deliminator string into record set:
CREATE function [dbo].[fnSpark_BreakUpList] (
#List VARCHAR(MAX)
)
RETURNS #csvlist TABLE (Item VARCHAR(MAX))
AS
BEGIN
DECLARE #Item VARCHAR(MAX)
-- Loop through each item in the comma delimited list
WHILE (LEN(#List) > 0)
BEGIN
IF CHARINDEX(',',#list) > 0
BEGIN
SET #Item = SUBSTRING(#List,1,(CHARINDEX(',', #List)-1))
SET #List = SUBSTRING(#List,(CHARINDEX(',', #List) + DATALENGTH(',')),DATALENGTH(#List))
END
ELSE
BEGIN
SET #Item = #List
SET #List = NULL
END
-- Insert each item into the csvlist table
INSERT into #csvlist (Item) VALUES (#Item)
END
RETURN
END
GO
I will post answers shortly to show how to increase the performance by using CHARINDEX. (So you dont have to anything like above....)
If you are not actually want to retrieve the item from the LONG delimited string, but to only filtering it, CHARINDEX is a better way to go for.
Instead of using IN Clause, you could just use:
WHERE CHARINDEX(','+TableA.School+',',','​+#School+',') > 0
NOTE:
1. I pad an extra comma at the end of target string 'TableA.School' to avoid the satuation that if a big string contains a sub string same as the filtering item.
(Such as we have a school called 'AB' and another 'ABC' that we dont want the 'ABC' to be picked up when we are targeting for 'AB'.... )
I pad an extra comma at the end of resources string​ '#School' to ensure that the single item / the last item (they will end without a comma) to be picked up when we are targeting them.
I pad an extra comma at the begining of target string 'TableA.School' to avoid the satuation that if a big string contains a sub string same as the filtering item.
(Such as we have a school called 'AB' and another 'CAB' that we dont want the 'CAB' to be picked up when we are targeting for 'AB'.... )
Example:
I am using:
WHERE
CHARINDEX(','+CAST(DENTIST4.wStudentYear AS VARCHAR(10))+',',','+#StudentYear+',') > 0
TO replace:
WHERE
DENTIST4.wStudentYear IN (#StudentYear)
for one of the report i was doing, which makes 4000+ pages rendering improved from about 10 mins into under a min for a large database (11 G).
IMPORTANT NOTES:
Please make sure the filter report parameters you are passing into the dataset uses JOIN clause.
=Join(Parameters!MyParameter.Value,",")​
Hope this helps....
IMPORTANT: This approach ONLY improves performance if the filter has large set of items, for any filters with small set of items IN clause will do better jobs.

Replacing data in database

I get a sheet of data that gets updated values daily, I am trying to write the updated data daily into the database to replace the data from the previous day. The database is Mysql. Also is it possible to use the replace function to do this?
Not sure if I fully understand your question, but to replace data you need a key for each row in your sheet.
Than you should
UPDATE <target_table> SET column1 = value1_from_sheet
WHERE key = key_col_from_sheet
REPLACE is a string function in TSQL and you can not use it to replace rows in a table. It is useful to replace some chars inside a string.
DECLARE #string nvarchar(50);
SET #string = "someValue";
SELECT REPLACE(#string,'Va','XX') => 'someXXlue'