User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 402,420 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,041 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 1961 | Replies: 4
Reply
Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

search

  #1  
Nov 11th, 2005
I have asp.net C# web application I want to search jobvacancy details using jobrole.
if user did not select any value I want to select all the details including null values.

this Jobvacancy table has JobRole feild and it allow to insert null values,

this is my stored procedure
CREATE PROCEDURE JobVacancy
(
@JobRole varchar (50)
)
as

Select NoOfVacancies,JobRole from JobVacancy where JobRole LIKE @JobRole + '%' 

when I use @JobRole value as '%' then if JobRole feild has null value it did not select.
but I want to select all the values including null values. how can I do this?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2005
Location: Manchester, UK
Posts: 481
Reputation: pty is on a distinguished road 
Rep Power: 3
Solved Threads: 31
pty's Avatar
pty pty is offline Offline
Posting Pro in Training

Re: search

  #2  
Nov 13th, 2005
select
NoOfVacancies,
JobRole
from JobVacancy
where (JobRole like @JobRole + '%')
or (JobRole is null)
Reply With Quote  
Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

Re: search

  #3  
Nov 13th, 2005
Thanks for your help using your ideas finally I manage to write a correct query,
CREATE PROCEDURE JobVacancy
(
@JobRole varchar (50)
)
as

if @JobRole <> '%'
begin 
Select NoOfVacancies,JobRole from JobVacancy where JobRole LIKE @JobRole+ '%' 
end
else
begin 
Select NoOfVacancies,JobRole from JobVacancy where (JobRole LIKE @JobRole+ '%' OR JobRole is null)
end
:evil:
Reply With Quote  
Join Date: Oct 2005
Location: Manchester, UK
Posts: 481
Reputation: pty is on a distinguished road 
Rep Power: 3
Solved Threads: 31
pty's Avatar
pty pty is offline Offline
Posting Pro in Training

Re: search

  #4  
Nov 16th, 2005
no problem, but with your solution i don't understand why you use an if..else statement here

Originally Posted by aish
if @JobRole <> '%'
begin 
Select NoOfVacancies,JobRole from JobVacancy where JobRole LIKE @JobRole+ '%' 
end
else
begin 
Select NoOfVacancies,JobRole from JobVacancy where (JobRole LIKE @JobRole+ '%' OR JobRole is null)
end

im sure you'd get the same result by just saying

select NoOfVacancies, JobRole
from JobVacancy
where JobRole like @JobRole + '%'
or JobRole is null

if @JobRole is '%' your statement would say '..where JobRole like '%%' which is the same as saying where JobRole like '%'; both solutions would work but I think you may be overcomplicating slightly.

on the right track though
Reply With Quote  
Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

Re: search

  #5  
Nov 19th, 2005
your correct I can get the result with out using if else. but then for ex. if I want to select 'Programmer' then JobRole=='programmer' and JobRole=='null' selected but I want to select JobRole=='null' only when I put JobRole=='%' otherwise I don't want to select null values.
:o
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 2:11 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC