How to get Concat comma separated value from mysql stored procedure or function? -


below table , stored procedure list of offdays comma separated value:-

offdaystable offdayid |sundayoff| mondayoff|tuesdayoff|wensdayoff|thursdayoff|  fridayoff| saturadyoff  1          1          1         1           0          0            0           0   create procedure getoffdays () begin declare offdays varchar(25); set offdays = '';   select  offdays =  case sundayoff when 1 '1' else '' end,   offdays = case  mondayoff when 1     case length(ltrim(rtrim(offdays))) when 0 '2' else concat(offdays,',' ,'2') end else offdays end   ,offdays = case  tuesdayoff when 1   case length(ltrim(rtrim(offdays))) when 0 '3' else concat(offdays,',' ,'3') end else offdays end   ,offdays = case  wednesdayoff when 1   case length(ltrim(rtrim(offdays))) when 0 '4' else concat(offdays,',' ,'4') end else offdays end   ,offdays = case  thursdayoff when 1   case length(ltrim(rtrim(offdays))) when 0 '5' else concat(offdays,',' ,'5') end else offdays end   ,offdays = case  fridayoff when 1   case length(ltrim(rtrim(offdays))) when 0 '6' else concat(offdays,',' ,'6') end else offdays end   ,offdays = case  saturdayoff when 1   case length(ltrim(rtrim(offdays))) when 0 '7' else concat(offdays,',' ,'7') end else offdays end school_information;   select offdays; end; 

my stored procedure returning nothing can find out out result like:-

offdays  1,2,3 

untested, think should give want:

create procedure getoffdays () begin declare offdays varchar(25); set offdays = '';   select  concat(case sundayoff when 1 '1,' else '' end,     case  mondayoff when 1 '2,' else '' end,     case  tuesdayoff when 1 '3,' else '' end,     case  wednesdayoff when 1 '4,' else '' end,     case  thursdayoff when 1 '5,' else '' end,     case  fridayoff when 1 '6,' else '' end,     case  saturdayoff when 1 '7,' else '' end) school_information offdays;   select if(right(offdays,1) = ',',left(offdays,length(offdays) -1),offdays) offdays; end; 

you need use in mysql if using select (set allows = b).


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -