How to remove unconvertable characters to ascii with SELECT in mySQL - mysql

I use a CONVERT function in my SELECT statement in order to avert utf8 errors, but MySQL leaves question marks behind. Is there a way to convert the unconvertable characters to blank or space characters?
SELECT MeetId,
ResId,
Special,
CONVERT(proposal USING ascii) as Proposal,
Analysis,
Vote,
Vote_for,
Oppose,
Discret,
Abstain,
gpVote %s
FROM RESO
WHERE RESO.MeetId = %s
As an example a typical result may have this in a field: 'The current issue ?A? is on the table '

What about just using REPLACE:
SELECT
REPLACE(CONVERT('§123' USING ascii), '?', '')
And the Fiddle.
Good luck.

Be careful, sgeddes solution also removes all question marks(if exist) from your string!
For example :
SELECT REPLACE(CONVERT('§How are you?' USING ascii), '?', '')
Output will be : How are you

Related

removing whitespaces not working MySQL. Used TRIM() and REPLACE()

Take a look : FIDDLE
select IF((TRIM(replace(' IKECHUKWU OSUJI',' ',''))=TRIM(replace('IKECHUKWU OSUJI','
',''))),"same","diff");
select IF((TRIM(replace(' Aman Minhas ',' ',''))=TRIM(replace(' Aman Min has','
',''))),"same","diff");
The first query returns diff. The second returns same. Its some weird spacing issue, cant seem to understand why this behaviour.
Your first string has a tab in it:
select IF((TRIM(replace(' IKECHUKWU OSUJI',' ',''))
^ this is actually a tab in the Fiddle
You can get rid of it with an additional REPLACE:
REPLACE(REPLACE(myString, ' ', ''), '\t', '')
The \t is a special literal. Other special literals such as newline or ASCII NUL may impact you as well. Literals are listed here.

MySql Script to search all non ascii and reaplace with space

Can I ask for some sort of script to search for an ascii ranging from DLE way up to US. You can check it at ASCII TABLE.
I have a code which suppposedly search for a non ascii character but I also want to replace it with a space at the same time.
Here's my code
SELECT *
from
`TABLE_NAME`(Sample only)
where COLUMN_NAME(sample only)
like %non-keyboard character(sample only(1st non keyboard character))% or
like %non-keyboard character(sample only(2nd non keyboard character))%.....
The code above is long since I used it from DLE down to US.
Any advice will be greatly appreciated.
You can search range from DLE to US using REGEXP. With REGEXP you can specify non-ascii character like [.DLE.].
SELECT col
FROM tab
WHERE col REGEXP '[[.DLE.]-[.US.]]';
But, Unforfunately there is no way to replace using REGEXP something like REG_STR_REPLACE(). please refere to this
So you should call REPLACE() several times but this is slow. You have three choices.
1.
SELECT REPLACE(REPLACE(REPLACE(col, CHAR(16), ''), CHAR(17), ''), CHAR(18), '')
FROM ...
2. install udf explained in SO
3. do in client side
Try this one
update tablename
set columnToCheck = replace(columnToCheck , char(146), '');
or
UPDATE tablename
SET columnToCheck = REPLACE(CONVERT(columnToCheck USING ascii), '?', '')
WHERE ...
Font: http://www.xaprb.com/blog/2006/04/14/bad-character-data-in-mysql/

How to replace (”); }); character

When I run the following query, it doesn't work.
UPDATE wp_posts
SET post_content = REPLACE (post_content, '(”); });','text here');
This does work correctly, may be you are using incorrect number of spaces.
mysql> select REPLACE ( 'this is (”); });' , '(”); });','SPARTAAA!!!!');
+---------------------------------------------------------------+
| REPLACE ( 'this is (”); });' , '(”); });','SPARTAAA!!!!') |
+---------------------------------------------------------------+
| this is SPARTAAA!!!! |
+---------------------------------------------------------------+
In wordpress,it might be a case that the strings are stored as html entity (eg " <) instead of actual character,hence you are not able to replace it.
I would suggest you to use this plugin : http://wordpress.org/plugins/search-and-replace/
Did you really mean the "smart quote" ”, and not the quotation mark "?
Are those really what's in your database?
[This answer was brought to you today in the form of a rhetorical question.]
If you are searching for these characters and replacing it one by one then you can use:
replace(replace(replace('tes;(', '(', ''), ')', ''), ';', '').
So if any of the three characters found; it will replace it.
And if you are looking for whole search as string then what you are doing is working fine.

Cleaning out a field of Phone numbers in mySql

In not a database guy but: I have mixed up data in a mySql database that I inherited.
Some Phone numbers are formatted (512) 555-1212 (call it dirty)
Others 5125551212 (Call it clean)
I need a sqlstamet that says
UPDATE table_name
SET Phone="clean'(Some sort of cleaning code - regex?)
WHERE Phone='Dirty'
Unfortunately there's no regex replace/update in MySQL. If it's just parentheses and dashes and spaces then some nested REPLACE calls will do the trick:
UPDATE table_name
SET Phone = REPLACE(REPLACE(REPLACE(REPLACE(Phone, '-', ''), ')', ''), '(', ''), ' ', '')
To my knowledge you can't run a regexp to replace data during the update process. Only during the SELECT statement.
Your best bet is to use a scripting language that you're familiar with and read the table and change it that way. Basically by retrieving all the entries. Then using a string replace to match a simple regexp such as [^\d]* and remove those characters. Then update the table with the new value.
Also, see this answer:
How to do a regular expression replace in MySQL?

Format Phone Numbers in MySQL

I have a bunch of phone numbers in a DB that are formatted as such: (999) 123-3456.
I'm needing them to look like 123-123-1234
Is there any sort of regex or something I can do in MySQL to quickly format all these phone numbers?
Also, frustratingly, some are NOT formatted like that, so I couldn't just apply this to an entire column.
Thanks!
A quick solution would be to run these two queries:
UPDATE table_name set PhoneCol = REPLACE(PhoneCol, '(', '');
UPDATE table_name set PhoneCol = REPLACE(PhoneCol, ') ', '-');
Just write a small php script that loops through all the values and updates them. Making that change is pretty simple in php. Then just run an update on the row to overwrite the value.
maybe a two pass solution.
strip out all non-numeric characters (and spaces)
inset the formatting characters '(',')', ' ', and '-' into the correct spots
(or better yet, leave them off and format only during select on your reports.)
I had a similar problem, but increased by the reason that some phones had the format with the dashes and others did not and this was the command that helped me to update the formats of the numbers that did not have the hyphens.
Phone before the command: 1234567890
Phone after command: 123-456-7890
The phone field is called phone_number and is a VARCHAR
The command I used is:
UPDATE database.table
SET phone_number = concat(SUBSTRING(phone_number,1,3) , '-' , SUBSTRING(phone_number,4,3) , '-' , SUBSTRING(phone_number,7,4))
WHERE LOCATE('-', phone_number) = 0;
I think your command could be like this:
UPDATE database.table
SET phone_number = concat(SUBSTRING(phone_number,2,3) , '-' , SUBSTRING(phone_number,7,8));
I would remove the WHERE clause under the assumption that all phones would be formatted with the (). Also, the second string of characters would start from position 7 because there appears to be a space after the parentheses.