Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with a small application and I want to filter all the customer on multiple conditions such as dob, order, advance. I'm using where condition. It is not working very well. I want if I enter DOB then display all the customer according to DOB if i enter all the 3 option then search all the condition like google Advanced Search.
Posted
Updated 19-Jan-11 23:17pm
v2

hi
Here i am giving you one example

SQL
declare @name varchar(100)='b',
@dob date='2010-12-10'


if(@name  is null and @dob is null)
BEGIN
    select * from tblUsers
    END
    else if(@name is not null and @dob is null)
    BEGIN
    select * from tblUsers where tblUsers.strFirstName like @name+'%'
    END
    else if(@name is null and @dob is not null)
    BEGIN
    select * from tblUsers where tblUsers.dateDateOfBirth=@dob
    END
    else
    BEGIN
        select * from tblUsers where tblUsers.strFirstName like @name+'%' and tblUsers.dateDateOfBirth=@dob
        END



I hope this idea may help you
 
Share this answer
 
Comments
Ankur\m/ 20-Jan-11 6:54am    
This is fine if number of fields are less. It will get messy when the fields increase.
This forum discusses the same thing: http://forums.asp.net/t/1381718.aspx[^]

I hope this helps!
 
Share this answer
 
Comments
Sandeep Mewara 21-Jan-11 0:56am    
Good link!
Ankur\m/ 21-Jan-11 1:01am    
Thanks Sandeep!
Sharing your query can give us little idea towards but I think you aren't putting condition well with AND and OR.

See THIS[^] my answer will guide you towards if you are making any mistake with condition.

Please share your code too.
 
Share this answer
 
Comments
Ankur\m/ 21-Jan-11 0:06am    
I don't know what's the problem with you Hiren. You are ready for 5 votes for you good answers but why don't you accept down votes. Read the question again and then read your answer. It nowhere answers the question. Still I didn't gave you a 1.
All answers here except yours answers the question. The link that I have given answers it the best. Thanks for your 1 vote, I will remember that.
Hiren solanki 21-Jan-11 0:31am    
I haven't voted you down. Sorry.
Ankur\m/ 21-Jan-11 0:35am    
A platinum authority member has voted it down. I am quite regular here and know people very well. Anyways thank you for replying.
Hiren solanki 21-Jan-11 0:44am    
How much point it deducted from you ?
Following are sample procedure code. You have to make your own code from this sample.

SQL
CREATE PROCEDURE [dbo].[usp_Get_Procedure_Name]
(
   @dob date,
   @order int
)
AS
BEGIN
declare @strSql1 nvarchar(4000)
set @strSql1 = 'Select * from table tb where 1=1'
if @dob is not null
begin
set @strSql1 = @strSql1 + ' and tb.date between '+@dob+''
end
if @order <> 0
begin
set @strSql1 = @strSql1 + ' and  tb.order = '+CAST(@order as varchar(20))+''
end
exec sp_executesql @strSql1
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900