Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Dynamically search a first name + last name column

Author  Topic 

robc
Yak Posting Veteran

60 Posts

Posted - 2008-08-01 : 15:44:26
Hi, i'm having trouble creating a query that will search by firstname/lastname. My criteria is as follows;

- must search for likeness of first or last names ie. 'r','c' will bring back records
- must be able to pass an empty string in case the user only wants to search by last name or first name

(@nameFirst IS NULL OR nameFirst like '%' + @nameFirst + '%') and
(@nameLast IS NULL OR nameLast like '%' + @namelast + '%')


i had this, but if i pass either parameter a null, it will bring back all records.

thanks in advance.

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2008-08-01 : 15:47:50
namefirst like '%' + Coalesce(@NameFirst,namefirst) + '%'
and
namelast like '%' + Coalesce(@NameLast,nameLast) + '%'
Go to Top of Page

robc
Yak Posting Veteran

60 Posts

Posted - 2008-08-01 : 15:54:17
Thanks Vinnie,

You are an sql GOD!
Go to Top of Page
   

- Advertisement -