I have a database column with multiple names separated by line breaks like this:
jones, sue
jones, mark
I want to generate a query that keeps the first line and discards the rest, no matter how many lines there are...
I though I was getting close with this:
SELECT
name1
FROM
email
WHERE
name1 REGEXP '.*(\r\n)?'
but not quite...
1 Answer 1
SELECT SUBSTRING_INDEX(col, "\r\n", 1) ...
Test case:
mysql> SELECT SUBSTRING_INDEX("jones, sue\r\njones, mark", "\r\n", 1);
+---------------------------------------------------------+
| SUBSTRING_INDEX("jones, sue\r\njones, mark", "\r\n", 1) |
+---------------------------------------------------------+
| jones, sue |
+---------------------------------------------------------+
answered Nov 6, 2018 at 2:15
-
hmmmm. thanks, but that doesn't seem to work... Is it possible that \n and \r (tried both...) don't work with my particular data? the column is utf-8 encoded...C.Lamb– C.Lamb2018年11月06日 10:09:11 +00:00Commented Nov 6, 2018 at 10:09
-
1@C.Lamb - I added a test case. If you still have troubles, please provide your failing case. (ascii/latin1/utf8 -- all work the same in this example.)Rick James– Rick James2018年11月06日 15:45:02 +00:00Commented Nov 6, 2018 at 15:45
lang-sql