Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top