Domanda

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.

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top