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)
 conditional clause

Author  Topic 

dmcgiv
Starting Member

5 Posts

Posted - 2006-01-12 : 22:46:25

When I'm writing a query where I have a conditional clause that is a varchar, or other fixed character type that canot be NULL is it more efficient to query by " = '%' " or " = field name" ? If you know of a better way that either option below please let me know.

Below is a demo of what I'm trying to do.


CREATE PROCEDURE TownSearch
(
@StartsWith char,
@Town varchar(50)
)

....

option 1:

WHERE
dbo.Company.CompanyName LIKE
CASE
WHEN @StartsWith = '~' THEN '[0-9]%'
WHEN @StartsWith IS NULL THEN dbo.Company.CompanyName
ELSE @StartsWith + '%'
END
AND
dbo.Address.Town LIKE
CASE
WHEN @Town IS NULL THEN dbo.Address.Town
ELSE @Town
END



option 2:

WHERE
dbo.Company.CompanyName LIKE
CASE
WHEN @StartsWith = '~' THEN '[0-9]%'
WHEN @StartsWith IS NULL THEN '%'
ELSE @StartsWith + '%'
END
AND
dbo.Address.Town LIKE
CASE
WHEN @Town IS NULL THEN '%'
ELSE @Town
END

X002548
Not Just a Number

15586 Posts

Posted - 2006-01-12 : 22:51:53
WHERE COALESCE(Col1,'') IN (@Col1,'')



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page
   

- Advertisement -