0

I have a string @x='2,12,13,1' and now i have to find out whether 2 is exists in the string @x. I have tried using of length function as below.

@st=2;
if((LENGTH(@x) - LENGTH(REPLACE(@x, @st, '')))/LENGTH(@st)=1)
    return 1;
else return 0;

if it returns 1 then string exists but it is not working as expected. pls let me know is their any other method implement this feature. Thanks.

  • http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad/3653574 – e4c5 Nov 23 '16 at 07:47
  • Can you use LOCATE? http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_locate – phobia82 Nov 23 '16 at 07:51

1 Answers1

0

We can use FIND_IN_SET function.

FIND_IN_SET(@st,@x); This function will return the position of the needle string. If it is >0 then string exists else it's not.