title: Making wildcards work in a variable MySQL Stored Procedure
body: <p>I wrote a Stored Procedure, that uses a variable with the sql query to make it dynamic depending on a set of parameters, my problem is when I try the LIKE clause with % wildcards, the query is not filtering, I tested with a regular query and it works. </p>
  
  <pre><code>CREATE DEFINER=`root`@`localhost` PROCEDURE `seleccionar_exp_repGral`(in p_fecha date, in p_fecha2 date, in p_compania int, in p_institucion int, in p_sector int, 
  in p_clasificacion int, in p_analista int, in p_decision int, in p_fuenteFinanciamiento int, in p_tipo int, in p_objeto varchar(3500))
  BEGIN
     set @qrystr = "select Expe.exp, Expe.mes, Expe.anio, Expe.fecha_recibido, cia.nombre_com, inst.nombre_inst, Expe.monto_egreso,
     Expe.anio_egreso, Expe.monto_ingreso, Expe.anio_ingreso, an.nombre_ana, deci.nombre_dec, Expe.fecha_sesion, sec.nombre_sec, 
     clas.nombre_cla, finan.nombre_fin, tipo.nombre_tipo from expediente Expe
     left outer join compania cia on (Expe.compania = cia.idcompania)
     left outer join institucion inst on (Expe.institucion = inst.idinstitucion)
     left outer join analista an on (Expe.analista = an.idanalista)
     left outer join decision deci on (Expe.decision = deci.iddecision)
     left outer join sector sec on (Expe.sector = idsector)
     left outer join clasificacion clas on (Expe.clasificacion = idclasificacion)
     left outer join financiamiento finan on (Expe.financiamiento = finan.idfinanciamiento)
     left outer join tipo_expediente tipo on (Expe.tipo = tipo.idtipo_expediente)";
  
  -- variables for filter
  set @pcia = p_compania;
  set @pinst = p_institucion;
  set @psector = p_sector;
  set @pclasificacion = p_clasificacion;
  set @panalista = p_analista;
  set @pdecision = p_decision;
  set @pfuenteFinanciamiento = p_fuenteFinanciamiento;
  set @ptipo = p_tipo;
  set @pObjeto = p_objeto;
  
  -- concatenate conditions for filter
  set @qrystr = CONCAT(@qrystr,'where Expe.fecha_recibido &gt;=\'',p_fecha,'\' and Expe.fecha_recibido &lt;=\'',p_fecha2,
  '\'');
  -- Filters
  IF @pcia &gt; 0 THEN
      set @qrystr = CONCAT(@qrystr,' and Expe.compania = ',@pcia);
  end if;
  IF @pinst &gt; 0 THEN
      set @qrystr = CONCAT(@qrystr,' and Expe.institucion = ',@pinst);
  end if;
  IF @psector &gt; 0 THEN
      set @qrystr = CONCAT(@qrystr,' and Expe.sector = ',@psector);
  end if;
  IF @pclasificacion &gt; 0 THEN
      set @qrystr = CONCAT(@qrystr,' and Expe.clasificacion = ',@pclasificacion);
  end if;
  IF @panalista &gt; 0 THEN
      set @qrystr = CONCAT(@qrystr,' and Expe.analista = ',@panalista);
  end if;
  IF @pdecision &gt; 0 THEN
      set @qrystr = CONCAT(@qrystr,' and Expe.decision = ',@pdecision);
  end if;
  IF @pfuenteFinanciamiento &gt; 0 THEN
      set @qrystr = CONCAT(@qrystr,' and Expe.financiamiento = ',@pfuenteFinanciamiento);
  end if;
  IF @ptipo &gt; 0 THEN
      set @qrystr = CONCAT(@qrystr,' and Expe.tipo = ',@ptipo);
  end if;
  IF @pObjeto &lt;&gt; "" THEN
      set @qrtystr = CONCAT(@qrystr," and Expe.objeto LIKE '",@pObjeto,"'");
  end if;
  -- preparamos el ultimo statement del query
  set @qrystr = CONCAT(@qrystr,' order by Expe.idexpediente asc');
  -- preparamos datos finales
  PREPARE stmt from @qrystr;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
  END$$
  </code></pre>
  
  <p>From that long SP I have this part: </p>
  
  <pre><code>    IF @pObjeto &lt;&gt; "" THEN
       set @qrtystr = CONCAT(@qrystr," and Expe.objeto LIKE '",@pObjeto,"'");
      end if;
  </code></pre>
  
  <p>This line I added the wildcard % before the @pObjeto and after that and it's not working, I even added wildcard to the variable, and my query still shows all registers without the text filtering.</p>
  
  <p>I wonder if it has to do with string formatting or something.</p>
  
  <p>I appreciate any help.</p>