Solved

Where clause in stored procedure

Posted on 2016-09-30
8
37 Views
Last Modified: 2016-09-30
Hello,

 Ihave a where clause in SP ,is a better way of writing this :
WHERE(
					[TravelDate] = @TravelDate OR 
					([TravelDate] is null AND @TravelDate is null) OR
					([TravelDate] is null AND @TravelDate ='') OR
					([TravelDate] ='' AND @TravelDate is null))

Open in new window


Cheers
0
Comment
Question by:RIAS
  • 4
  • 3
8 Comments
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 100 total points
Comment Utility
Hi
Please consider something similar to the following:

WHERE(
					[TravelDate] = @TravelDate OR
					(ISNULL([TravelDate],'') = '' AND ISNULL(@TravelDate,''))
) 

Open in new window

0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 400 total points
Comment Utility
Only need to perform a single test:
WHERE ISNULL([TravelDate],'') = ISNULL(@TravelDate,'')

Open in new window

1
 

Author Closing Comment

by:RIAS
Comment Utility
Cheers!
0
 

Author Comment

by:RIAS
Comment Utility
Will there be performance difference if I modify the Stored Procedure with

WHERE ISNULL([TravelDate],'') = ISNULL(@TravelDate,'')

Open in new window

?

Cheers
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
I don't think so.
How long took the original solution and how long it takes after you used mine suggestion?
0
 

Author Comment

by:RIAS
Comment Utility
Can't notice the difference,
Is it worth changing the original code then as I will have to modify nearly 100 Stored Procedures?
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
I guess TravelDate is not indexed, right?
IMO doesn't worth the work. It's mostly code reduction than performance gain.
Take it as code tuning lesson ;)
1
 

Author Comment

by:RIAS
Comment Utility
Cheers!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 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

9 Experts available now in Live!

Get 1:1 Help Now