Postgresql unaccent() equivalent in MySQL - mysql

i have to compare two strings in a query like following:
SELECT *
FROM MY_TABLE
WHERE column LIKE '%keyword%';
But i want to compare unaccented values of both column and keyword. Is there an unaccent() function or other way to achieve this in MySQL?

AFAIK, No there is no unaccent() function present in MySQL. To ignore the accent you will have to set the proper collation for the column you are trying to compare. Example: How to remove accents in MySQL?

Related

Truncate decimal for MySql Command

I want to use Truncate command with Select * query . I am thinking if something like a nested query is applicable just to determine if the returned column is a decimal value or not and apply truncate (2 decimal point) to it.
A random version would look something like following -
Select * from my_Table ( Truncate if column = intiger)
Please provide pointers as on how to develop such kind of Query.

SELECT DISTINCT not working

Why does this query produce an "Duplicate entry" error?
TRUNCATE parim_firm_tag_names;
INSERT INTO parim_firm_tag_names (firm_tag_name_value)
SELECT DISTINCT sona
FROM parim_marksona;
Error message:
SQL Error (1062): Duplicate entry '1-??????? ??????' for key
'firm_tag_name_value'
As you can see, firm_tag_name_value has an unique index, I use DISTINCT select and I'm truncating all existing data from tag_names.
What could produce this error?
This could be happening because of different collations defined on both tables parim_firm_tag_names and parim_marksona as string comparisons using distinct may results in different values on case sensitive and case insensitive collation values.
You can check collation of columns using this query:
SHOW FULL COLUMNS FROM parim_marksona;
SHOW FULL COLUMNS FROM parim_firm_tag_names;
To avoid this error, you can convert collation of column sona to the collation of column firm_tag_name_value using COLLATE, while selecting the distinct values from table parim_marksona.
Assuming collation of column firm_tag_name_value as latin1_swedish_cs:
TRUNCATE parim_firm_tag_names;
INSERT INTO parim_firm_tag_names (firm_tag_name_value)
SELECT DISTINCT sona COLLATE latin1_swedish_cs
FROM parim_marksona;
This should work without errors.
For more details refer manual Column Character Set and Collation.
Different character sets between the two tables, perhaps?

Illegal Mix Of Collations While Comparing Datetime with String

I have created a MYSQL stored procedure that searches all columns of a table (parameter 1) to find if any of them contains a string (parameter 2). The problem is that when I compare a datetime column with a string that contains greek characters I get the following error:
SELECT * FROM my_table WHERE my_datetime LIKE '%ΕΛΛΗΝΙΚΑ%';
Illegal mix of collations for operation 'like'
The execution of the query is successful when I use latin characters. I know how to avoid checking columns based on their datatype, however I really want to be able to search for datetime strings as well. I use MySQL 5.5.24. The collation of the database is utf8_general_ci and the collation of the server is latin1_swedish_ci. I have tried the command SET NAMES utf8 before the query, with no luck though. Any ideas? Thanks.
I had a problem with similar query:
SELECT * FROM some_table WHERE some_date LIKE '%2012-01%';
The result was an "Illegal mix of collations" error.
Using simply DATE_FORMAT function helped:
SELECT * FROM some_table WHERE DATE_FORMAT(some_date, '%Y-%m-%d') LIKE '%2012-01%';

How to detect and replace non-printable characters from table?

I have imported data from xls file into table. but there are some garbage (non ascii charactors).
I want to remove those non printable characters from database.
here is the query i found which can select the entries which has non-ascii characters
select * from TABLE where COLUMN regexp '[^ -~]';
But how can i remove those characters from table using mysql query or procedure ?
Please give suggestions.
thanks in advance.
Since the question is about "detect and replace" I wouldn't suggest the Delete query from #TheWitness. Instead I would do something like this:
UPDATE some_table SET some_column = REGEXP_REPLACE(some_column, '[^ -~]', '') WHERE some_column REGEXP '[^ -~]'
The query above will use regular expression to search for the particular characters and with REGEXP_REPLACE it will replace them with empty string.
More on REGEXP_REPLACE
It's fairly simple, you just change your SELECT into a DELETE as follows
DELETE FROM TABLE WHERE COLUMN regexp '[^ -~]';

How to check if a row in a table has muti-byte characters in it in a MySQL database?

I have a table which has column of descr which stores string values. Some of the values in descr has multi-byte characters in it and I want to know all those rows so I can remove those characters. How can I query the tables using MySQL functions to determine which rows have multi-byte characters. I am using MySQL version 5.1
SELECT ...
FROM yourtable
WHERE CHAR_LENGTH(descr) <> LENGTH(descr)
char_length is multi-byte aware and returns the actual character count. Length() is a pure byte-count, so a 3-byte char returns 3.
have you tried the collation and CHARSET functions on your descr column?
You can find the description of this functions here:
http://dev.mysql.com/doc/refman/5.1/en/information-functions.html
I think for your need it fits better the COERCIBILITY function. You can do something like:
select COERCIBILITY(descr, COLLATE utf8) from myTable;
and if this function returns 0 then you must edit the line.