Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ALl,

I am facing the issue in framing the logic for writing the query.
There are three textboes like FName,Lname,Mobno if user enters any one data and click search the data matching firstname should come. Similarly if he inputs Fname and lname and click serach it should result matching Fname and Lname together.
ACtually the table is as below
Fname |Lname |mobileNo
prashant|P|888
prashant| s|1234
What I want if they GIVE fname=prahant and search for it both the rows should come but if they give Fname=Prashan and lname=p only first row should come.

I am unable to achieve this after spending so many hours. Can some one pls advise me the logic
Regards
Prashant
Posted
Updated 28-Nov-14 21:17pm
v3
Comments
Tomas Takac 28-Nov-14 6:23am    
Can you show us what you have so far?
Shweta N Mishra 28-Nov-14 6:36am    
does your data field in table contains both firstname and lastname in same column ?
Prashant Bangaluru 29-Nov-14 3:17am    
SOrry for the late reply ..I just updated the table structure actually they are 2 different coumns.

Create a stored procedure (or query) with all three parameters and use it like this:

SQL
@FName varchar(100)
@LName varchar(100)
@Mobno varchar(20)

SET @FName = '%' + ISNULL(LTRIM(RTRIM(@FName)), '') + CASE WHEN @FName IS NULL THEN '' ELSE '%' END

-- repeat the above for other two variables
-- what you're doing is: set your variable to '%word%' which will enable to look for partial information or just % (that is, generic look for everything)

SELECT your-columns-list
FROM
    your-table
WHERE
    FName LIKE @Fname
AND
    LName LIKE @Lname
AND
    Mobno LIKE @Mobno



If you have to have exact match you can somewhat simplify

SQL
SET @FName = NULLIF(LTRIM(RTRIM(@Fname)), '')
-- repeat for other two
-- this ensures that your variable is null if it is empty string

SELECT your-column-list
FROM
your-table
WHERE
(FName = @Fname OR @Fname IS NULL)
AND 
(LName = @LNAme OR @LName IS NULL)
AND
(Mobno = @Mobno OR @Mobno IS NULL)



Finally, you could just do dynamic SQL, using only the variable you have in WHERE clause and calling sp_execute[^] to get the result.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
Prashant Bangaluru 29-Nov-14 3:19am    
I sinisa thanks for ur reply.. Looks like ur solution meets my requirement I will try and update you.
Prashant Bangaluru 30-Nov-14 23:23pm    
Hello Sinisa, the second solution helped me to fix my issues.. THanks for your help..
Hi,

Your can try like this...

SQL
SELECT your-column-list
FROM
your-table
WHERE
FName+Lname+Mobno like '%your textbox values%'


Suppose you have space between all three fields..

then your put space between them like .

SQL
SELECT your-column-list
FROM
your-table
WHERE
FName+' '+Lname+' '+Mobno like '%your textbox values%'
 
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