search

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

search

 
0
  #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
  1. CREATE PROCEDURE JobVacancy
  2. (
  3. @JobRole varchar (50)
  4. )
  5. AS
  6.  
  7. 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 523
Reputation: pty is on a distinguished road 
Solved Threads: 37
pty's Avatar
pty pty is offline Offline
Posting Pro

Re: search

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

Re: search

 
0
  #3
Nov 13th, 2005
Thanks for your help using your ideas finally I manage to write a correct query,
  1. CREATE PROCEDURE JobVacancy
  2. (
  3. @JobRole varchar (50)
  4. )
  5. AS
  6.  
  7. IF @JobRole <> '%'
  8. begin
  9. SELECT NoOfVacancies,JobRole FROM JobVacancy WHERE JobRole LIKE @JobRole+ '%'
  10. end
  11. else
  12. begin
  13. SELECT NoOfVacancies,JobRole FROM JobVacancy WHERE (JobRole LIKE @JobRole+ '%' OR JobRole IS NULL)
  14. end
:evil:
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 523
Reputation: pty is on a distinguished road 
Solved Threads: 37
pty's Avatar
pty pty is offline Offline
Posting Pro

Re: search

 
0
  #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
  1. IF @JobRole <> '%'
  2. begin
  3. SELECT NoOfVacancies,JobRole FROM JobVacancy WHERE JobRole LIKE @JobRole+ '%'
  4. end
  5. else
  6. begin
  7. SELECT NoOfVacancies,JobRole FROM JobVacancy WHERE (JobRole LIKE @JobRole+ '%' OR JobRole IS NULL)
  8. 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 Quick reply to this message  
Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

Re: search

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC