19

In one table of my database I have strings which looks like this one:

sometext-othertext

How to remove the text including dash with SELECT statement so the result to be just sometext?

5 Answers 5

34

Return the substring before the first occurrence of the delimiter "-":

SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 1) as result;

Outputs result = "foo"

You can replace 1 with the numbers of occurrences you want before getting the substring

SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 2) as result;

Outputs result = "foo-bar"

Reference: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

Sign up to request clarification or add additional context in comments.

1 Comment

9

SELECT REPLACE("sometext-othertext","-", "") as newvalue

This should do it.
Edit:

  SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1) as newvalue;

apologies for not understanding the question earlier.

2 Comments

No, this will produce sometextothertext while OP wants sometext.
Sorry - replied hastily I guess! SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1) as newvalue;
5

have two ways to use troubleshooting this, if you like to display all (only just remove "-" symbol, i recommended use this because have more speed:

SELECT REPLACE('sometext-othertext','-','');

that syntax change text "-" to "" (empty text because you want just remove that)

if you want to display only part one (ex :sometext) or just part two (ex :othertext) you can use this, for spliting string:

SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1);

Comments

1

just use :

SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1);

Function have 3 prameters, first that is a text, second is object for spliting, and number (1) for getting you want (the controller)

Comments

0

better use mysql REPLACE function to replace "-" with ""

2 Comments

That is not the entire goal. Everything following the first dash should be removed.
Sorry, I misunderstood the question.

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.