SQL No Como Sentencia no funciona


Tengo el siguiente código dentro de un procedimiento almacenado.

WHERE
    WPP.ACCEPTED = 1 AND
    WPI.EMAIL LIKE '%@MATH.UCLA.EDU%' AND
    (WPP.SPEAKER = 0 OR
    WPP.SPEAKER IS NULL) AND
    WPP.COMMENT NOT LIKE '%CORE%' AND
    WPP.PROGRAMCODE = 'cmaws3'

La instrucción NOT LIKE no funciona, y sí, antes de que alguien diga algo, hay elementos con la columna COMENTARIO que no incluye CORE y todas las demás columnas están bien.

¿Alguien sabe qué tiene de malo esto?

Author: MPelletier, 2009-10-28

5 answers

Si WPP.COMMENT contiene NULL, la condición no coincidirá.

Esta consulta:

SELECT  1
WHERE   NULL NOT LIKE '%test%'

No devolverá nada.

En una columna NULL, tanto LIKE como NOT LIKE contra cualquier cadena de búsqueda devolverán NULL.

¿Podría por favor publicar valores relevantes de una fila que en su opinión deberían ser devueltos pero no lo es?

 47
Author: Quassnoi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-28 16:55:39

¿El valor de tu columna de COMENTARIOS en particular es null?

A veces NO COMO no sabe cómo comportarse correctamente alrededor de nulls.

 6
Author: James Cronen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-28 16:56:36

Acabo de encontrarme con el mismo problema y lo resolví, pero no antes de encontrar este post. Y viendo que su pregunta no fue realmente respondida, aquí está mi solución (que con suerte funcionará para usted, o para cualquier otra persona que busque lo mismo que hice;

En lugar de;

... AND WPP.COMMENT NOT LIKE '%CORE%' ...

Try;

... AND NOT WPP.COMMENT LIKE '%CORE%' ...

Básicamente mover el "NO" al otro lado del campo que estaba evaluando funcionó para mí.

 5
Author: Valiante,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-03 14:24:36

Acaba de venir a través de esto, la respuesta es simple, utilice ISNULL. SQL no devolverá filas si el campo que está probando no tiene valor (en algunos de los registros) al hacer una búsqueda de comparación de texto, por ejemplo:

WHERE wpp.comment NOT LIKE '%CORE%'

Entonces, tiene que sustituir temporalmente un valor en los registros null (vacíos) usando el comando ISNULL, por ejemplo

WHERE (ISNULL(wpp.comment,'')) NOT LIKE '%CORE%'

Esto mostrará todos sus registros que tienen nulos y omitirá cualquiera que tenga sus criterios coincidentes. Si quisieras, podrías poner algo en las comas para ayudarte recuerde, eg

WHERE (ISNULL(wpp.comment,'some_records_have_no_value')) NOT LIKE '%CORE%'
 3
Author: Paul D. Starbrook,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-31 12:54:23

Mattgcon,

Debería funcionar, ¿obtiene más filas si ejecuta el mismo SQL con la línea "NO ME GUSTA" comentada? Si no, compruebe los datos. Sé que lo mencionó en su pregunta, pero verifique que la instrucción SQL real esté usando esa cláusula. Las otras respuestas con NULL también son una buena idea.

 1
Author: Mark Kadlec,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-28 16:57:40