0

I have a table of course :

enter image description here

I want to fetch data from searching based on columns Country, university, level, interest and substream.

Query which I've tried but not getting better result.

select *
from edu_college_desc
where (country = @country and
       university = @university and
       leveln = @level and
       interest = @interest and
       substream=@substream)
   or (country = @country or
       university = @university or
       leveln = @level or
       interest = @interest or
       substream = @substream) 

What I want to do is: if select only country then the data should come based on only country or if I select only stream then data fetched based from stream only if I select both or more then data fetch should be based on those columns.

How can I get perfect results?

1
  • Use ColName LIKE '%YourValue% instead of ColName = YourValue Commented May 4, 2016 at 9:23

4 Answers 4

2

Try this method,

select * from edu_college_desc 
where   country     =   ISNULL(@country ,country)
    and university  =   ISNULL(@university ,university)
    and leveln      =   ISNULL(@level ,leveln)
    and interest    =   ISNULL(@interest ,interest
    and substream   =   ISNULL(@substream,substream)

In this you can pass the value NULL to any of the parameters if it is not selected.(ie If you have set value only for @university and others are NULL, then result will be university = @university)

Sign up to request clarification or add additional context in comments.

7 Comments

abdul is that possible without using the value null i can get the result.
use default NULL while declaring this parameters, then no need to pass or assign this.
i tried but not getting results. In sql query is working perfectly but when i run this query into website and passing the parameters then i'm not getting result because the parameters are passing ' ' blank value not null.
what is the datatype of parameters ? try with Null-able Int datatype. Otherwise try to pass DBNULL instead of ' ' blank value.
i passed DBNULL also and other thing also but still it is passing blank value i'm using dropdownlist in asp.net c# from there i'm selecting value and i passed null value on first index of DDL.
|
2

For each category, check if the parameter is null (not given) or the same as specified:

select *
from edu_college_desc
where (@country is null or country = @country)
  and (@university is null or university = @university)
  and etc...

And, of course, you can also use MS SQL Server's ISNULL, just as in Abdul Rasheed answer. Convenient but less portable.

Comments

1

Your query should be like :

SELECT * FROM edu_college_desc WHERE (@country IS NULL OR country = @country) AND (@university IS NULL OR university = @university) AND (@level IS NULL OR level = @level ) AND (@interest IS NULL OR interest = @interest) AND (@substream IS NULL OR substream= @substream)

Comments

0

This code will definitely help you.

SELECT * FROM edu_college_desc WHERE (country = @country or isnull(@country,0)= 0) AND (university = @university or isnull(@university,0)= 0) AND (leveln = @leveln or isnull(@leveln,0)= 0) AND (interest = @interest or isnull(@interest,0)= 0) AND (substream=@substream or isnull(@substream,0)= 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.