1

I have a query in SQL that looks more or less like this:

select replace('Hello World', 'World', '') -- Returns  Hello 

Now I want to use same function with utf-8 characters like this:

 select replace('Hello Wórld', 'World', '') -- Should Return Hello, but returns Hello Wórld

Note that I already have the collation set to utf-8 case insesitive;

following query returns true:

select 'ó' = 'o' -- Returns 1

Is there a quick way to make REPLACE function work as I expect?

Thank you.

EDIT

I´m posting the real piece of code where I build the query, I didn´t think that it could help, but anyway, here it is:

...
self::$sql.="
        0";
        foreach ( $fields as $field=>$weight) { 

            foreach ( $keywords as $keyword ) { 
                self::$sql.="
                +COALESCE((LENGTH($table.$field) - LENGTH(REPLACE(LOWER($table.$field), LOWER('$keyword'), '')) )
                *$weight/LENGTH('$keyword'), 0)
                *COALESCE((LENGTH($table.$field) - LOCATE('$keyword', $table.$field))/LENGTH($table.$field), 0)
            ";
        }
    }
    self::$sql.=" AS weight";

...

2
  • The docs say Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str. i would assume you need to lower case everything for it to work
    – exussum
    Oct 23, 2014 at 11:28
  • Indeed, I do, the query I posted is a simpler version of the real one, that uses LOWER before REPLACE, and doesn´t work neither
    – sergio0983
    Oct 23, 2014 at 11:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.