Showing posts with label surname. Show all posts
Showing posts with label surname. Show all posts

Friday, February 10, 2012

Concatenating fields

Hi,

there's a method to concatenate fields in a WHERE clause?

I've a parameter which represents a name and surname of a person; in the table I've two fields representing one the name and the other the surname. I'd like to do a "LIKE" comparison concatenating Name and Surname field and confronting with my parameter... Is it possible?

Yes, it's possible.

WHERE Name + ' ' + Surname LIKE @.param

This may be another option:

WHERE Name LIKE @.param OR Surname LIKE @.param

Jos

|||

Like this?

DECLARE @.NAMESTABLE (idint IDENTITY(1,1), Forenamevarchar(10), Surnamevarchar(10))DECLARE @.SEARCHNAMEvarchar(20)SET @.SEARCHNAME ='Mark Smith'INSERT @.NAMESVALUES ('Mark','Smith')SELECT *FROM @.NAMESWHERE Forename +' ' + Surname = @.SEARCHNAME
|||

Jos has the solution... Tnx

Concatenating fields

Hi,

I've a table with two fields representing one Name and the other teh Surname of a persona. I've a to create a Stored Procedure with one input parameter that is a string containing Name and Surname (I don't know in waht order...)

What I'd like to do is to concatenate teh fields Name and Surname and confronting with "LIKE" in the "WHERE" clause... something like this:

Select Name, Surname FROM XXX where (Name + ' ' + Surname LIKE @.parameter OR Surname + ' '+ Name LIKE @.parameter).

The problem is that I don't know neither if it is possible neither the correct syntax...

If there are two similar fields in your database, then you probably just want to check both fields somehing like this:

SELECT Name, Surname FROM XXX WHERE (Name LIKE (@.Name + '%')) AND (Surname LIKE (@.Surname + '%'))

Is this what you're looking for?