944,116 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 2626
  • MS SQL RSS
Nov 11th, 2005
0

search

Expand Post »
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
MS SQL Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
aish is offline Offline
26 posts
since Aug 2005
Nov 13th, 2005
0

Re: search

select
NoOfVacancies,
JobRole
from JobVacancy
where (JobRole like @JobRole + '%')
or (JobRole is null)
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Nov 13th, 2005
0

Re: search

Thanks for your help using your ideas finally I manage to write a correct query,
MS SQL Syntax (Toggle Plain Text)
  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:
Reputation Points: 10
Solved Threads: 0
Light Poster
aish is offline Offline
26 posts
since Aug 2005
Nov 16th, 2005
0

Re: search

no problem, but with your solution i don't understand why you use an if..else statement here

Quote originally posted by aish ...
MS SQL Syntax (Toggle Plain Text)
  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
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Nov 19th, 2005
0

Re: search

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
Reputation Points: 10
Solved Threads: 0
Light Poster
aish is offline Offline
26 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: user creation using msde
Next Thread in MS SQL Forum Timeline: database reminders





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC