1

I have written mysql code like

substr(a_code,1,ifnull(if((locate("-",a_code)>0),locate("-",a_code)-1,length(a_code)),0))

What it does is i have code like abcd-1 or abcd or abcd-2. I want to take out code part before -. It can be abcd or abcd-1 or abcd-n. So code is working fine. Can i make it in a better way.

1 Answer 1

2

You could write it this way:

substring_index(a_code,'-',1)

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

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.