Case-insensitive REPLACE in MySQL?

后端 未结 8 2165
無奈伤痛
無奈伤痛 2020-12-05 10:00

MySQL runs pretty much all string comparisons under the default collation... except the REPLACE command. I have a case-insensitive collation and need to run a

8条回答
  •  臣服心动
    2020-12-05 10:34

    In the previous answers, and the pento.net link, the arguments to LOCATE() are lower-cased.

    This is a waste of resources, as LOCATE is case-insensitive by default:

    mysql> select locate('el', 'HELLo');
    +-----------------------+
    | locate('el', 'HELLo') |
    +-----------------------+
    |                     2 |
    +-----------------------+
    

    You can replace

    WHILE Locate( LCASE(REPLACE_THIS), LCASE(REPLACE_WHERE), last_occurency ) > 0 DO

    with

    WHILE Locate(REPLACE_THIS, REPLACE_WHERE, last_occurency ) > 0 DO

    etc.

提交回复
热议问题