Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Include NULL data in comparison

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-06-06 : 09:23:27
Kristian writes "My records have data ranges (StartDate and EndDate).
StartDate and/or EndDate can be NULL in the table.

I want to execute a SELECT statement with the current date and include records with nulls.

Here is my first WHERE clause:
--
WHERE ( StartDate = NULL OR StartDate < @ChangeDate )
AND ( EndDate = NULL OR @ChangeDate < EndDate )
--

Is this the most efficient way, or is there a NULL setting I can use. ( I did consider using ISNULL() but this would perform more comparisons)

regards
Kris"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-06 : 09:27:20
When you compare with NULL use IS instead of =

WHERE ( StartDate IS NULL OR StartDate < @ChangeDate )
AND ( EndDate IS NULL OR @ChangeDate < EndDate )
--


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

kristianwhittick
Starting Member

12 Posts

Posted - 2006-06-06 : 10:50:19
Sorry, that was not my question.
The = was a typing mistake.

My real question is,
Is there a better syntax that is more efficient ?

i.e. somthing like this (this is a guess)

WHERE ISNULL(StartDate < @ChangeDate, TRUE)
AND ISNULL(@ChangeDate < EndDate, TRUE)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-06-06 : 10:56:36
You can also try this.

WHERE StartDate < coalesce( @ChangeDate, StartDate )
AND EndDate > coalesce( @ChangeDate, EndDate )


Not sure if this is more efficient than what madhivanan posted. Give it a try.


KH

Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-06-06 : 12:55:35
According to this...

http://www.sommarskog.se/dyn-search.html (search for 'Alternate Key Lookup'), it might be better (i.e. more efficient) to do this...

WHERE ( StartDate IS NOT NULL AND StartDate < @ChangeDate )
AND ( EndDate IS NOT NULL AND @ChangeDate < EndDate )

It's a funny old world




Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page
   

- Advertisement -