﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / comparing NULLs / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 17:09:10 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>Duh, my bad Matt.  I had a brain cramp.  A primary key cannot contain a nullable column.  I was thinking unique constraints which would be a natural or conceptual primary key.  So this code will only allow the first insert to complete.[code]CREATE TABLE null_primary_key_test    (    id INT IDENTITY(1,1),    null_column INT NULL CONSTRAINT uc_null_column UNIQUE     ) GO  INSERT INTO null_primary_key_test ( 	null_column )  SELECT NULL  -- the 2 causes the batch to execute twice GO 2  DROP TABLE null_primary_key_test [/code]</description><pubDate>Thu, 02 Apr 2009 18:35:46 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>RE: comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>Jack - I have to ask - are you talking about conceptual PK's?  I'm pretty sure you cannot have anything NULL in a primary key, at least not one actually enforced by SQL Server.  Just curious.</description><pubDate>Thu, 02 Apr 2009 18:04:56 GMT</pubDate><dc:creator>Matt Miller (#4)</dc:creator></item><item><title>RE: comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>That actuallly makes sense as NULL's are also treated as equal in primary key's as well.</description><pubDate>Thu, 02 Apr 2009 05:04:24 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>RE: comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>While not available in SQL2000, the INTERSECT and EXCEPT operators compare NULLs as being equal even with ANSI_NULLS on.</description><pubDate>Thu, 02 Apr 2009 03:11:40 GMT</pubDate><dc:creator>Ken McKelvey</dc:creator></item><item><title>RE: comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>[quote][b]JacekO (4/1/2009)[/b][hr]Not always it depends on ANSI_NULLS settingThis is from books online:Care must be taken when comparing null values. The behavior of the comparison depends on the setting of the SET ANSI_NULLS option.When SET ANSI_NULLS is ON, a comparison in which one or more of the expressions is NULL does not yield either TRUE or FALSE; it yields UNKNOWN. This is because a value that is unknown cannot be compared logically against any other value. This occurs if either an expression is compared to the literal NULL, or if two expressions are compared and one of them evaluates to NULL. For example, this comparison always yields UNKNOWN when ANSI_NULLS is ON:ytd_sales &amp;gt; NULLThis comparison also yields UNKNOWN any time the variable contains the value NULL:ytd_sales &amp;gt; @MyVariableUse the IS NULL or IS NOT NULL clauses to test for a NULL value. This can add complexity to the WHERE clause. For example, the Region column in the Northwind Customers table allows null values. If a SELECT statement is to test for null values in addition to others, it must include an IS NULL clause:SELECT CustomerID, CompanyName, RegionFROM Northwind.dbo.CustomersWHERE Region IN ('WA', 'SP', 'BC')   OR Region IS NULLTransact-SQL supports an extension that allows for the comparison operators to return TRUE or FALSE when comparing against null values. This option is activated by setting ANSI_NULLS OFF. When ANSI_NULLS is OFF, comparisons such as ColumnA = NULL return TRUE when ColumnA contains a null value and FALSE when ColumnA contains some value besides NULL. Also, a comparison of two expressions that have both evaluated to null values yields TRUE. With ANSI_NULLS set OFF, this SELECT statement returns all the rows in the Customer table for which Region is a null value:SELECT CustomerID, CompanyName, RegionFROM Northwind.dbo.CustomersWHERE Region = NULLRegardless of the ANSI_NULLS setting, Null values are always considered equal for the purposes of the ORDER BY, GROUP BY, and DISTINCT keywords. Also, a unique index or UNIQUE constraint that allows NULL can contain only one row with a NULL key value. A subsequent row with NULL is rejected. A primary key cannot have NULL in any column that is part of the key.Computations involving NULL evaluate to NULL because the result must be UNKNOWN if any of the factors is unknown. For example, column1 + 1 evaluates to NULL if column1 is NULL.When the columns being searched include those defined as allowing null values, you can find null or nonnull values in the database with this pattern:WHERE column_name IS [NOT] NULL[/quote]A niggly comment first:  NULL is not a value, it's the absence of any value, so "null value" doesn't compute.  In a database sense NULL is a "state", and not any kind of value.With that in mind, what ANSI_NULLS does is to allow you to WRITE "variable is NULL" as "Variable=NULL".  [u]It does NOT allow you do perform actual comparisons [/u]against  NULLS, nor comparisons between two variables in a null state, etc....  Any unary operations on that statement will trigger the parser to treeat that as a comparison, and thus will fail.Try this script on for size so you can get a better idea.  Just toggle between ANSI_NULLS OFF and ANSI_NULLS ON at the top.[code]use testingset ansi_nulls ON;declare @n1 varchar(20)declare @n2 varchar(20)set @n1=nullset @n2='h'select case when @n1=@n2 then 'true' else 'false' endselect case when NOT @n1=@n2 then 'true' else 'false' endselect case when @n1&amp;gt;=@n2 then 'true' else 'false' endselect case when @n1&amp;lt;=@n2 then 'true' else 'false' endselect case when @n1&amp;lt;&amp;gt;@n2 then 'true' else 'false' endset @n2=nullselect 'switch'select case when @n1=@n2 then 'true' else 'false' endselect case when NOT @n1=@n2 then 'true' else 'false' endselect case when @n1&amp;gt;=@n2 then 'true' else 'false' endselect case when @n1&amp;lt;=@n2 then 'true' else 'false' endselect case when @n1&amp;lt;&amp;gt;@n2 then 'true' else 'false' end[/code][code][/code]</description><pubDate>Wed, 01 Apr 2009 15:22:48 GMT</pubDate><dc:creator>Matt Miller (#4)</dc:creator></item><item><title>RE: comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>In addition to the above you may want to check out the below linkhttp://www.sqlservercentral.com/articles/Advanced+Querying/2829/</description><pubDate>Wed, 01 Apr 2009 14:14:34 GMT</pubDate><dc:creator>Vijaya Kadiyala</dc:creator></item><item><title>RE: comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>If your server/session has ANSI_NULLS ON then any comparison to a NULL value results in Unknown, not true or false.  ANSI_NULLS is ON by default in SQL Server so you should use IS NULL/IS NOT NULL when doing comparison to NULL values.See [url=http://msdn.microsoft.com/en-us/library/aa196339(SQL.80).aspx]NULL Comparison Search Conditions[/url] in BOL.Edit: JacekO beat me to it.</description><pubDate>Wed, 01 Apr 2009 07:01:36 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>RE: comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>Not always it depends on ANSI_NULLS settingThis is from books online:Care must be taken when comparing null values. The behavior of the comparison depends on the setting of the SET ANSI_NULLS option.When SET ANSI_NULLS is ON, a comparison in which one or more of the expressions is NULL does not yield either TRUE or FALSE; it yields UNKNOWN. This is because a value that is unknown cannot be compared logically against any other value. This occurs if either an expression is compared to the literal NULL, or if two expressions are compared and one of them evaluates to NULL. For example, this comparison always yields UNKNOWN when ANSI_NULLS is ON:ytd_sales &amp;gt; NULLThis comparison also yields UNKNOWN any time the variable contains the value NULL:ytd_sales &amp;gt; @MyVariableUse the IS NULL or IS NOT NULL clauses to test for a NULL value. This can add complexity to the WHERE clause. For example, the Region column in the Northwind Customers table allows null values. If a SELECT statement is to test for null values in addition to others, it must include an IS NULL clause:SELECT CustomerID, CompanyName, RegionFROM Northwind.dbo.CustomersWHERE Region IN ('WA', 'SP', 'BC')   OR Region IS NULLTransact-SQL supports an extension that allows for the comparison operators to return TRUE or FALSE when comparing against null values. This option is activated by setting ANSI_NULLS OFF. When ANSI_NULLS is OFF, comparisons such as ColumnA = NULL return TRUE when ColumnA contains a null value and FALSE when ColumnA contains some value besides NULL. Also, a comparison of two expressions that have both evaluated to null values yields TRUE. With ANSI_NULLS set OFF, this SELECT statement returns all the rows in the Customer table for which Region is a null value:SELECT CustomerID, CompanyName, RegionFROM Northwind.dbo.CustomersWHERE Region = NULLRegardless of the ANSI_NULLS setting, Null values are always considered equal for the purposes of the ORDER BY, GROUP BY, and DISTINCT keywords. Also, a unique index or UNIQUE constraint that allows NULL can contain only one row with a NULL key value. A subsequent row with NULL is rejected. A primary key cannot have NULL in any column that is part of the key.Computations involving NULL evaluate to NULL because the result must be UNKNOWN if any of the factors is unknown. For example, column1 + 1 evaluates to NULL if column1 is NULL.When the columns being searched include those defined as allowing null values, you can find null or nonnull values in the database with this pattern:WHERE column_name IS [NOT] NULL</description><pubDate>Wed, 01 Apr 2009 07:00:34 GMT</pubDate><dc:creator>JacekO</dc:creator></item><item><title>comparing NULLs</title><link>http://www.sqlservercentral.com/Forums/Topic687844-8-1.aspx</link><description>Hi Masters,    I have a question about comparing strings and number with columns that have NULLs inside.If i compare a column that as a NULL value with a column that as a integer or a varchar, the result is allways false. Correct?Anything that i compare with a NULL (including other NULL) will always be avaliated as false.Correct?Tks,  Pedro</description><pubDate>Wed, 01 Apr 2009 06:52:16 GMT</pubDate><dc:creator>pedro.ribeiro</dc:creator></item></channel></rss>