Checking database for NULL boolean value - sql

I have a field in a table that is boolean, a range of records have no value (not true or false). How do I write my SQL statement to find these?
I have tried the following SQL statements without success:
1) SELECT * FROM table WHERE field = NULL
2) SELECT * FROM table WHERE field != TRUE AND field != FALSE
Any help would be greatly appreciated.
Many Thanks, Ben

In TSQL, you need to use IS rather than = when comparing with NULL:
SELECT * FROM table WHERE field IS NULL

Try
select * from table where field IS null

You want IS NULL I believe:
SELECT * FROM table WHERE field IS NULL

Related

Sql query to find records with column value is blank

I'm trying to find records with Column value is blank. as you can see in the table I have records with following values when I fire a Distinct query on the Column MOVE_STU.
now I can find all the record with column value related to (1,2,3,4 and 6) but I'm not able to find the records with Column Value related to (5). as there are Thousands of record in the table i'm not able to figure out how should I write query in order to get these records. Kindly help. Thanks in Advance. :)
Use trim and comparison with empty string to get records that contain only whitespaces:
SELECT *
FROM your_table
WHERE LTRIM(RTRIM(MOVE_STU)) = ''
Try something like this:
SELECT * FROM YourTable WHERE LTRIM(RTRIM(YourField)) = ''
This will give you all matches that are empty, or have only whitespace.
How about this ?
SELECT * FROM YourTable WHERE MOVE_STU = ''
SELECT * FROM YourTable WHERE ISNULL(MOVE_STU,'') = ''
You can try this:
If your datatype is int then you have to change in varchar
ALTER TABLE [tbl_name] ALTER COLUMN [MOVE_STU] [VARCHAR(50)];
then run the query:
SELECT * FROM [tbl_name] WHERE MOVE_STU IS NULL;

Get the non empty data from a sql_varient column

I have a sql table with sql_varient column. I want to query the table to get the records which has value. I used following query
SELECT *
from Table
WHERE sql_varient_column IS NOT NULL
But its return the data which has no data also. How can I achieve this
To get the row for column sql_varient_column is not null and not empty:
SELECT *
from Table
WHERE NULLIF(sql_varient_column, '') IS NOT NULL
Try this one:
SELECT * FROM Table Where sql_varient_column != ''
I think you want this.
you can try it
SELECT *
from Table
WHERE ISNULL(sql_varient_column,'') != ''

SQL: Best way to write a condition that checks the value of a variable when the variable could be null

I am writing a pgplsql function in which I have a variable var that could be NULL or valorized. In a later query I do:
SELECT * FROM table WHERE column = var
that, in case of a NULL var, becomes
SELECT * FROM table WHERE column = NULL
So the query fails with NULL, because, as PostgreSQL documentation says,
The null value represents an unknown value, and it is not known whether two unknown values are equal
I resolved it with a CASE statement:
SELECT * FROM table WHERE
( CASE WHEN var IS NULL THEN column IS NULL ELSE column = var END ) = TRUE
But I am unsure wether this is the best way to resolve the question... do you have any good alternative?
SELECT * FROM table WHERE column IS NOT DISTINCT FROM var
If var has a value of NULL, do you really only want to select records having a column value of NULL? Or would you want to treat it as a 'select all'/'do not restrict'?
If the latter applies, then an approach like this would work. (This is pseudocode as I am native to MSSQL.)
SELECT * FROM table WHERE
(var IS NULL AND column = column) OR column = var

if condition in where statement

How do I write a query that if one column value is not null then compare the value with the querystring, else do not compare the column value.
for example, I have a tbl1 with programs1 and programs2 two columns, the value in these two columns can be null or with value. I want to have the if statement that say
if programs1 is not null, programs1=#programs and
if programs2 is not null, programs2=#programs
my query is like this:
select * from tbl1 where
(programs1 is not null and programs1=#programs)
and
(program2 is not null and programs2=#programs)
but it didn't work the way I want. How do I write a correct query for this case? TIA.
I believe you're looking for COALESCE.
WHERE COALESCE(programs1, programs2) = #Programs
However, if you'd like to compare only if it has a value:
-- If the column is null, ignore it. If it has a value, then
-- check that value.
WHERE (programs1 IS NULL OR programs1 = #Programs)
AND (programs2 IS NULL OR programs2 = #Programs)
Also, I assume you mean sql-server since you reference asp.net
ISNULL(programs1,programs2) = #Programs
or
(programs1 is not null and #Programs = programs1) or (programs2 is not null and #Programs = programs2)
select *
from tbl1
where (programs1=#programs or programs1 is null)
and (programs2=#programs or programs2 is null)

Select rows where column is null

How do you write a SELECT statement that only returns rows where the value for a certain column is null?
Do you mean something like:
SELECT COLUMN1, COLUMN2 FROM MY_TABLE WHERE COLUMN1 = 'Value' OR COLUMN1 IS NULL
?
I'm not sure if this answers your question, but using the IS NULL construct, you can test whether any given scalar expression is NULL:
SELECT * FROM customers WHERE first_name IS NULL
On MS SQL Server, the ISNULL() function returns the first argument if it's not NULL, otherwise it returns the second. You can effectively use this to make sure a query always yields a value instead of NULL, e.g.:
SELECT ISNULL(column1, 'No value found') FROM mytable WHERE column2 = 23
Other DBMSes have similar functionality available.
If you want to know whether a column can be null (i.e., is defined to be nullable), without querying for actual data, you should look into information_schema.
Use Is Null
select * from tblName where clmnName is null
You want to know if the column is null
select * from foo where bar is null
If you want to check for some value not equal to something and the column also contains null values you will not get the columns with null in it
does not work:
select * from foo where bar <> 'value'
does work:
select * from foo where bar <> 'value' or bar is null
in Oracle (don't know on other DBMS) some people use this
select * from foo where NVL(bar,'n/a') <> 'value'
if I read the answer from tdammers correctly then in MS SQL Server this is like that
select * from foo where ISNULL(bar,'n/a') <> 'value'
in my opinion it is a bit of a hack and the moment 'value' becomes a variable the statement tends to become buggy if the variable contains 'n/a'.
select Column from Table where Column is null;
select * from tableName where columnName is null
For some reasons IS NULL may not work with some column data type. I was in need to get all the employees that their English full name is missing, I've used:
SELECT emp_id, Full_Name_Ar, Full_Name_En
FROM employees
WHERE Full_Name_En = '' or Full_Name_En is null