| Author |
Topic |
|
lols
Posting Yak Master
174 Posts |
Posted - 2007-11-12 : 06:31:37
|
| Hi,I have a table employee with firstname and lastname columns.I have written an exact search query which matches the data entered by the user :CREATE PROCEDURE SearchEmployee@fnm nvarchar(20) ,@lnm nvarchar(22) = null,ASselect employeeid, employeecity from EmployeesWHERE firstname = @fnm AND lastname = @lnmThe problem is sometimes the user doesnt give lastname or at times, adds up both the first and last nm together. So the search doesnt work.I was thinking can I write a statement like this :select employeeid, employeecity from EmployeesWHERE name = @fnm + @lnmHow do I combine columns?thank you |
|
|
nduggan23
Starting Member
42 Posts |
Posted - 2007-11-12 : 06:34:01
|
| Just use an OR instead of AND. will do a combination of first and last name. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-12 : 06:36:59
|
According to your own specsquote: Originally posted by lols I have written an exact search query which matches the data entered by the user
E 12°55'05.25"N 56°04'39.16" |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-11-12 : 06:37:51
|
Something like this:select employeeid, employeecity from EmployeesWHERE name = @fnm + coalesce(' ' + @lnm, '')Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
nduggan23
Starting Member
42 Posts |
Posted - 2007-11-12 : 06:44:56
|
| Sorry miss read your post OP. where name = (@frm + ' ' + @lnm) |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-11-12 : 06:55:04
|
quote: Originally posted by nduggan23 Sorry miss read your post OP. where name = (@frm + ' ' + @lnm)
That won't work when @lnm is Null.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
nduggan23
Starting Member
42 Posts |
Posted - 2007-11-12 : 06:59:02
|
| Can you explain why please? or does it require ISNULL('',@lnm) to work? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-12 : 07:01:31
|
quote: Originally posted by nduggan23 Can you explain why please? or does it require ISNULL('',@lnm) to work?
Select 'a'+NULLMadhivananFailing to plan is Planning to fail |
 |
|
|
lols
Posting Yak Master
174 Posts |
Posted - 2007-11-12 : 07:06:26
|
| thanks for all your replies..I do not have a column called 'name'..the only columns are firstname and lastnameHow can i make this query work with the given columns?select employeeid, employeecity from EmployeesWHERE name = @fnm + coalesce(' ' + @lnm, '') |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-11-12 : 07:11:23
|
| select employeeid, employeecity from EmployeesWHERE (firstname = @fnm or @fnm is null)AND (lastname = @lnm or @lnm is null)_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-12 : 07:13:58
|
| select employeeid, employeecity from EmployeesWHERE firstname = @fnm OR lastname = @lnmMadhivananFailing to plan is Planning to fail |
 |
|
|
lols
Posting Yak Master
174 Posts |
Posted - 2007-11-12 : 07:24:34
|
| thanks again..It seems I have not being able to describe my problem correctly. I apologize. Let me try again :So let us assume i searched for the name 'John Matthew'. Now the same string can come in 2 forms..1.@fnm = 'John'@lnm = 'Matthew'2. @fnm ='John Matthew'@lnm = ''Using the 'OR' will search all results with last name as 'Matthew' which is not desired.I want the overall effect to be that of an exact search with of course the above requirement in place i.e sometimes the user doesnt give lastname or at times, adds up both the first and last nm togetherI tried this :select employeeid, employeecity from EmployeesWHERE firstname = @fnm + coalesce(' ' + @lnm, '') OR lastname = @lnmHow do I modify this? |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-11-12 : 07:55:32
|
| this would be one way...WHERE firstname + coalesce(' ' + lastname , '') = @fnm + coalesce(' ' + @lnm, '')_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-12 : 08:04:54
|
select employeeid, employeecity from EmployeesWHERE firstname LIKE '%' + @fnm + '%'OR lastname LIKE '%' + @lnm + '%' E 12°55'05.25"N 56°04'39.16" |
 |
|
|
lols
Posting Yak Master
174 Posts |
Posted - 2007-11-12 : 08:31:03
|
| Hi..will LIKE yield exact search..? I want exact search..i will try spirit's suggestion. |
 |
|
|
|