0

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

2
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                                              |
+---------------------------------------------------------+
2
  • 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... Commented 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.) Commented Nov 6, 2018 at 15:45

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.