Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select unicode character u2028 in mysql 5.1

I am trying to select unicdode character /u2028 in MySQL 5.1. MySQL 5.1 does support utf8 and ucs2.

In newer versions of MySQL i could select the char just be using utf16 or utf32 collation:

SELECT char(0x2028 using utf16);
SELECT char(0x00002028 using utf32);

But MySQL 5.1 do not support utf16 and utf32. How could I select the unicode character then?

Perhaps a few words about my use case: I have an third party application which stores data in a mysql database and using JavaScript for user interface. The application do not deal with the problem unicode characters /u2028 and /u2029 are valid JSON but will break JavaScript code. (For details see http://timelessrepo.com/json-isnt-a-javascript-subset) So I like to know how much data is affected by that issue and perhaps use replace on MySQL to fix it.


To demonstrate the problem:

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `string` varchar(100) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

INSERT INTO `test` (`id`, `string`) VALUES
(1, 'without U+2028'),
(2, 'with U+2028 at this "
    "point');

SELECT * FROM test WHERE string LIKE CONCAT("%", char(0x2028 using utf16), "%");
// returns row 2 as expected

SELECT * FROM test WHERE string LIKE CONCAT("%", char(??? using utf8), "%");
// U+2028 in utf8 is 0xE2 0x80 0xA8 isn't it?
// But how to parse this to char function?
like image 981
jelhan Avatar asked Jan 20 '14 12:01

jelhan


1 Answers

The unicode character U+2028 can be encoded in UTF-8 as hexadecimal e280a8. So the answer is to use the UNHEX function in MySQL to find it.

SELECT * FROM test WHERE string LIKE CONCAT("%", UNHEX('e280a8'), "%");

MySQL 5.1 can only handle characters encloded in UTF-8 up to three bytes long. So searching for U+2028 using UNHEX will work, but searching for U+1F600 won't as that takes up four bytes.

Use UNHEX('e280a9') to search for U+2029.

To find other characters, visit https://fileformat.info/info/unicode/char/2028/index.htm, substituting '2028' for the character you are looking for. Look for the the number in brackets in the 'UTF-8 (hex)' row.

like image 123
Tim Rogers Avatar answered Sep 27 '22 19:09

Tim Rogers