2011-03-28 21 views
28

quiero filtrar un registro ....de consultas SQL - ¿Cómo filtro nulo o no nulo

Si StatusID es nulo, el filtro del registro (donde StatusID no es nulo)

Si es StatusID no nulo, filtre el registro donde ID de estado es igual al ID de estado especificado.

¿Cómo puedo hacer esto?

Respuesta

45

tal como dijiste

select * from tbl where statusid is null 

o

select * from tbl where statusid is not null 

Si su StatusID no es nulo, entonces será seleccionado muy bien cuando se tiene un valor real, sin necesidad de ningún "si "lógica si eso es lo que estabas pensando

select * from tbl where statusid = 123 -- the record(s) returned will not have null statusid 

si desea seleccionar donde es nulo o un valor, tratar

select * from tbl where statusid = 123 or statusid is null 
+0

Pero, ¿cómo se hace "Si"? – 001

+0

ver mi última adición que contiene el "o" – JeremyWeir

+0

gracias, funciona ahora :) – 001

3

¿Qué tal statusid = statusid. Null nunca es igual a nulo.

0

creo que esto podría funcionar:

select * from tbl where statusid = isnull(@statusid,statusid) 
1
WHERE something IS NULL 

y

WHERE something IS NOT NULL 
1

conjunto ANSI_NULLS fuera ir seleccionar * de la tabla t combinación interna otherTable o en t.statusid = O .statusid ir establecer ansi_nulls en ir

0

Dondequiera que está intentando comprobar el valor NULL de una columna, se debe utilizar

IS NULL y no es nulo

No use = NULL o == NULL


Ejemplo (NULL)

select * from user_registration where registered_time IS NULL 

volverá las filas con registered_time valor es NULL


Ejemplo (NOT NULL)

select * from user_registration where registered_time IS NOT NULL 

devolverá las filas con registered_time valor es NOT NULL

Nota: La palabra clave null, not null y is son not case sensitive.

Cuestiones relacionadas