0

I have a MySQL table in which multiple values occurs in some rows. In the example below such values are separated by vertical bar (|).

+---------+------+
| cui     | type |
+---------+------+
| 100     | A    |
| 101|102 | A    |
| 103     | B    |
| 104|103 | C    |
+---------+------+

I wonder how to retrieve only the first value if multiple values are present. For example, the table above should be returned as

+---------+------+
| cui     | type |
+---------+------+
| 100     | A    |
| 101     | A    |
| 103     | B    |
| 104     | C    |
+---------+------+

Any suggestions are greatly welcome.

1 Answer 1

1

You can use the function substring_index()

mysql> select substring_index('101|102','|',1);
+----------------------------------+
| substring_index('101|102','|',1) |
+----------------------------------+
| 101                              |
+----------------------------------+

So the query becomes

select substring_index(cui,'|',1) as cui,
type
from table_name
Sign up to request clarification or add additional context in comments.

Comments

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.