Solved

'Simple' SQL Problem selecting null dates

Posted on 2009-05-06
4
440 Views
Last Modified: 2012-05-06
Hi,
This really should be simple, but I'm having strange behaviour on the following code:
This works fine (returns 31 rows):
DECLARE @numInstances int
EXEC      @numInstances = sp_executesql N'SELECT     COUNT(*) AS numInstances
FROM         xjrvw_TR_Instances
WHERE     (fkResponsiblePersonId = @ResponsiblePersonId) AND (fkOrganizationId = @OrganizationId) AND (TraceDate IS
NULL)',N'@ResponsiblePersonId int,@OrganizationId int',@ResponsiblePersonId=1,@OrganizationId=2

This returns 0 rows:
DECLARE @numInstances int
EXEC      @numInstances = sp_executesql N'SELECT     COUNT(*) AS numInstances
FROM         xjrvw_TR_Instances
WHERE     (fkResponsiblePersonId = @ResponsiblePersonId) AND (fkOrganizationId = @OrganizationId) AND (TraceDate IS
NULL)',N'@ResponsiblePersonId int,@OrganizationId int',@ResponsiblePersonId=1,@OrganizationId=2
GO
And this also returns 0 rows (from the SQL Profiler when I run through the app)
DECLARE @numInstances int
EXEC      @numInstances = sp_executesql N'SELECT     COUNT(*) AS numInstances
FROM         xjrvw_TR_Instances
WHERE     (fkResponsiblePersonId = @ResponsiblePersonId) AND (fkOrganizationId = @OrganizationId) AND (TraceDate IS
NULL)',N'@ResponsiblePersonId int,@OrganizationId int',@ResponsiblePersonId=1,@OrganizationId=2
GO

I have tried changing it to a store procedure, to no avail.  The problem is definitely with the TraceDate IS NULL  - it works without that part of the predicate. I tried TraceDate.ISNULL = 1 but it tells me 'Cannot call methods on datetime.'

Can anyone please help?  I need to run it through the app, which interprets the top code to the second version.  

Thanks!
Carol.

exec sp_executesql N'SELECT     COUNT(*) AS numInstances

FROM         xjrvw_TR_Instances

WHERE     (fkResponsiblePersonId = @ResponsiblePersonId) AND (fkOrganizationId = @OrganizationId) AND (TraceDate IS 

NULL)',N'@ResponsiblePersonId int,@OrganizationId int',@ResponsiblePersonId=1,@OrganizationId=2

GO

DECLARE @numInstances int

EXEC	@numInstances = sp_executesql N'SELECT     COUNT(*) AS numInstances

FROM         xjrvw_TR_Instances

WHERE     (fkResponsiblePersonId = @ResponsiblePersonId) AND (fkOrganizationId = @OrganizationId) AND (TraceDate IS 

NULL)',N'@ResponsiblePersonId int,@OrganizationId int',@ResponsiblePersonId=1,@OrganizationId=2

GO

DECLARE @ResponsiblePersonId int

SET @ResponsiblePersonId = 2

DECLARE @OrganizationId int

SET @OrganizationId=1

SELECT     COUNT(*) AS numInstances

FROM         xjrvw_TR_Instances

WHERE     (fkResponsiblePersonId = @ResponsiblePersonId) AND (fkOrganizationId = @OrganizationId) AND (TraceDate IS 

NULL)

Open in new window

0
Comment
Question by:jrepdev
  • 2
4 Comments
 
LVL 8

Expert Comment

by:vinurajr
ID: 24322582
try with isnull(TraceDate,1)=1 or TraceDate is null
0
 

Author Comment

by:jrepdev
ID: 24322712
Thanks.  Nice try, but that didn't work either...  It doesn't fail, just returns 0 rows instead of 31.  I could mention that this is being implemented via an .xsd using C#, not that it will help :-)

I appreciate your help,
Carol.
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 24322938
I hope this issue is with respect to your ANSI_NULL settings in SQL Server 2005.

When SET ANSI_NULLS is ON, a SELECT statement that uses WHERE column_name = NULL returns zero rows even if there are null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns zero rows even if there are nonnull values in column_name.

When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison operators do not follow the SQL-92 standard. A SELECT statement that uses WHERE column_name = NULL returns the rows that have null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns the rows that have nonnull values in the column. Also, a SELECT statement that uses WHERE column_name <> XYZ_value returns all rows that are not XYZ_value and that are not NULL.

Referred from http://msdn.microsoft.com/en-us/library/ms188048(SQL.90).aspx

If you have ANSI_NULLS ON, then use IS NULL in where condition
If you have ANSI_NULLS OFF, then use = NULL in where condition.
0
 

Author Closing Comment

by:jrepdev
ID: 31578853
Actually, my problem was much simpler - I'd transposed the values for the parameters!  However, that was a really interesting post - thank you - I didn't know that about Ansi Nulls.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now