if condition in where statement - sql

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)

Related

Why Select eliminating NULL records on a varchar comparison

I have a table with 782,856 records. There is a column PEOPLE_TYPE in this table that is varchar(20). I don't think table schema matters but if it does I will gladly post it.
It has these distinct values (parens is a count of each type):
NULL (782,101)
ANONYMOUS (1)
BOARD (530)
USER (224)
So why does this select return these results???
select * from people where PEOPLE_TYPE != 'BOARD'
This return 225 rows...USER & ANONYMOUS....why aren't my nulls included...because I have now performed a text search and NULLs can't really be compared so they are eliminated?
Thank You for your patience with my remedial question.
NULL is a strange thing. Any comparison with NULL is false:
NULL = NULL is false
NULL != anything is false
NULL != NULL is also false.
You have to say things like column is null, or column is not null.
Your query would need
select * from people where PEOPLE_TYPE != 'BOARD' or PEOPLE_TYPE is null
You can read this for details on why records with NULL are not being returned: http://msdn.microsoft.com/en-us/library/ms188048.aspx
If you want records with NULL to be returned you need to write the query like this:
select * from people where ISNULL(PEOPLE_TYPE, '0') != 'BOARD'
Or this:
select * from people where PEOPLE_TYPE != 'BOARD' OR PEOPLE_TYPE IS NULL
There is another thing called COALESCE, http://msdn.microsoft.com/en-us/library/ms190349.aspx
I use quite often,
SELECT * FROM People WHERE COALESCE(PEOPLE_TYPE, '') != 'BOARD'

How to display Items that equal a value in one column and are not null in another

Let's say I want to display all items in a table with following criteria how would i do this?
SELECT *
FROM TABLE
WHERE TABLE.COLUMN1 = 'example' AND TABLE.COLUMN2 != 'NULL'
I want it to display all values from COLUMN1. How does one go about this process in MS SQL?
NULL values can be compared using IS [NOT] NULL in SQL server. Please check this.
SELECT *
FROM TABLE
WHERE TABLE.COLUMN1 = 'example' AND TABLE.COLUMN2 IS NOT NULL
NULL is an UNKNOWN value , you cannot use any Comparison Operators (= , <> , > , <) with it. you check for nulls like
ColumnName IS NULL or ColumnName IS NOT NULL
If you think about it , it makes sense, to compare two or more values, you need to know the values only then you can compare them, Since SQL Server considers a NULL to be an UNKNOWN value, you cannot really compare an unknown value to anything.

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

Null Value Statement

I have created a table called table1 and it has 4 columns named Name,ID,Description and Date.
I have created them like Name varchar(50) null, ID int null,Description varchar(50) null, Date datetime null
I have inserted a record into the table1 having ID and Description values. So Now my table1 looks like this:
Name ID Description Date
Null 1 First Null
One of them asked me to modify the table such a way that The columns Name and Date should have Null values instead of Text Null. I don't know what is the difference between those
I mean can anyone explain me the difference between these select statements:
SELECT * FROM TABLE1
WHERE NAME IS NULL
SELECT * FROM TABLE1
WHERE NAME = 'NULL'
SELECT * FROM TABLE1
WHERE NAME = ' '
Can anyone explain me?
In a CREATE TABLE, the NULL or NOT NULL here varchar(50) null is a constraint that determines whether NULLs are allowed. NOT NULL means no.
When you inserted data, which statement did you run?
INSERT TABLE1 VALUES (Null, 1, First, Null)
or
INSERT TABLE1 VALUES ('Null', 1, First, 'Null')
The first one uses the keyword NULL, inserts a NULL (not a null value: no such thing, arguably). No values is stored except in the NULL bitmap fields
The second one has a string "null" and the characters N, U, L, L + 2 bytes for length are stored
When you run SELECT * FROM TABLE1, client tools will show NULL.
To test whether you actually have NULLs or the string NULL, run this
SELECT ISNULL(name, 'fish'), ISNULL(date, GETDATE()) FROM TABLE1
For the SELECTs
--null symbols. No value stored
SELECT * FROM TABLE1 WHERE NAME IS NULL
--string null
SELECT * FROM TABLE1 WHERE NAME = 'NULL'
--empty string
SELECT * FROM TABLE1 WHERE NAME = ' '
Note: null symbol/value is not empty string. It has no value and won't compare. Even to itself.
As for your DBA, the code above with ISNULL will decide what is stored.
Edit: if you are storing null symbol/value, then your DBA should read up on "null bitmap"
The data does represent nulls. The text 'Null' is your query tool displaying the text.
One of them asked me to modify the table such a way that The columns Name and Date should have Null values instead of Text Null. I don't know what is the difference between those.
The NULL keyword indicates the absence of any value -- the value is unknown.
But that won't stop someone from storing the letters that spell out "NULL", data type providing (which INT and DATETIME will not). Because of this, operators like IS NULL would not work on text that spells out "NULL" and vice versa -- searching for strings using: ... LIKE '%NULL%' will not return records with NULL values.
The data type of the column does matter with regard to NULL in SQL Server. In a UNION statement, you need to cast NULL to be the appropriate data type -- the default for NULL is INT:
SELECT CAST('2011-01-01 00:00:00' AS DATETIME)
UNION
SELECT CAST(NULL AS DATETIME)
Based on the information provided about the columns and the output, the DBA appears to be asking you to change the text the client you are using to connect to SQL Server with displays when a NULL value is encountered in a resultset. Reminds me of my first job dishwashing, and was asked to get the lefthanded spatula...
The string "Null" is a string.
The value of NULL (or Null or null, SQL is case-insensitive when it comes to these things) is used to denote an unknown value. It's the empty set of values, if you will.
http://www.w3schools.com/sql/sql_null_values.asp
NULL, in software, is symbolic of no value. Assuming you're inserting the columns using a string with null as the value, use the null constant. e.g.
INSERT INTO table1 (Name,ID,Description,Date) VALUES (NULL,1,'First',NULL);
Note that NULL is a constant in SQL, not the word "NULL" in a string.
AFAIC, there is no different between NULLs. There are different column types. But as long as a column is a text data type, and it's NULL, it's a text NULL.
Sometimes there are questions about empty strings ("") instead of NULLs, but the description you're using doesn't seem to be referring to that.
SELECT * FROM TABLE1 WHERE NAME IS NULL
Returns all rows where the Name is NULL
SELECT * FROM TABLE1 WHERE NAME = 'NULL'
Returns all rows where the Name is equal to the string 'NULL', Null values are not returned
SELECT * FROM TABLE1 WHERE NAME = ' '
Returns all rows where the Name is equal to exactly one space ' ', Null values are not returned
If you run this statement it might help clear up when its null and when its not
select
*,
case
WHEN name is null THEN 'Its Null alright'
ELSE 'It has a value'
END
FROM TABLE1

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