Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 24th, 2004, 03:45 PM
Makito Makito is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 2 Makito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
LIKE in a NULL field

Hi everyone,

I need to create a stored procedure that receives 7 paramaters, 6 of them are optional. The problem is that I am using a LIKE @param1 and sometimes the value in the tables for this @param1 is NULL. If the value the user provides for @param1 is null, that row is not going to be selected as the LIKE '%' for a NULL value is not selected. I could check for each param if @param1 is null, but since there are 6 of them, it is not practical to do so. I also tried adding LIKE @param1 OR @param1 is null, but then it causes me a problem when a value is provided cause it selects all the rows that has @param1 as NULL!! I really don't know how to deal with this.

Using SQL Server 2000 and C# ASP.NET

Thank you!

Reply With Quote
  #2  
Old November 24th, 2004, 04:00 PM
MrData MrData is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: Sacramento, California, USA
Posts: 13 MrData User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 11 sec
Reputation Power: 0
2 ways I can think of...

Well, probably more than 2 ways, but here goes:

Code:
  
  IF @param1 IS NULL
  BEGIN
  
  SELECT ...
  FROM ....
  WHERE param1_column is null
  
  END
  ELSE
  BEGIN
  
  SELECT ...
  FROM ...
  WHERE param1_column LIKE '%" + @param1 + '%'
  
  END
  
  


That attacks your problem directly, but lengthens your code and may cause issues with how well SQL caches the execution plan for your proc.

My other idea is:

Code:
  
  DECLARE PROCEDURE @param1 varchar(10) = 'null'
  AS
  BEGIN
  
  SELECT ...
  FROM ...
  WHERE isnull(param1_column, 'null') like '%' + @param1 + '%'
  
  END
  
  


But that may not work as well depending on how the calling app takes it. Try either one, and I'm curious to see if they work.

Hope that helps,
Steve

Last edited by MrData : November 24th, 2004 at 04:01 PM. Reason: Forgot we're dealing with LIKE here...sorry

Reply With Quote
  #3  
Old November 25th, 2004, 10:21 AM
Salem Salem is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Dublin, Ireland
Posts: 157 Salem User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 10 m
Reputation Power: 6
If when you say @param1 is null you mean is = '' then

select...
from ...
where ((field1 is null and @param1 = '') or field1 like '%' + @param1 + '%')

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > LIKE in a NULL field


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway