문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top