Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to combine firstname and lastname in SQL and search with LIKE

Tags:

c#

sql

asp.net

I would like to combine FirstName and LastName into one column called 'FULL NAME' and search with LIKE in SQL.

Example 1:

FirstName : Milan
LastName: Patel

FullName: Milan Patel

Search with: Like '%SomeText%'

Example 2:

Title: " Meetings: A practical alternative to work."

Search: I would like to search the entire word within the give string. ex: work should return the above title.

Here is what I have so far.

SELECT 
    CE.Title, CC.FirstName + ' ' + CC.LastName AS FullName
FROM 
    EntryTable CE
JOIN 
    UserTable CC ON CE.EntryId = CC.USerId
WHERE 
    CC.FirstName LIKE 'n%'

EDIT

I am getting it there slowly. search is working fine with this query.

    SELECT 
     CE.Title 
    FROM 
     EntryTable CE
    JOIN 
     UserTable CC
    ON 
     CE.EntryId = CC.USerId
    WHERE 
     CC.FirstName  + ' ' + CC.LastName LIKE 's%'

BUT it only search for name starting with 's', i would like to search name starting, ending, contains conditions as well. how can i go about that. please help

like image 713
patel.milanb Avatar asked Jun 14 '13 09:06

patel.milanb


People also ask

How do I combine first name and Last Name in SQL query?

Let's say you want to create a single Full Name column by combining two other columns, First Name and Last Name. To combine first and last names, use the CONCATENATE function or the ampersand (&) operator.

Can you combine in and like in SQL?

Is there as way to combine the "in" and "like" operators in Oracle SQL? Answer: There is no direct was to combine a like with an IN statement.

How do I find similar names in SQL?

To find the duplicate Names in the table, we have to follow these steps: Defining the criteria: At first, you need to define the criteria for finding the duplicate Names. You might want to search in a single column or more than that. Write the query: Then simply write the query to find the duplicate Names.


1 Answers

You can use LIKE with concatenated column values as below

WHERE 
  CC.FirstName + ' ' + CC.LastName LIKE 's%'

BUT it only search for name starting with 's', i would like to search name starting, ending, contains conditions as well. how can i go about that. please help

Then you have to use % before and after search string like below

WHERE 
  CC.FirstName + ' ' + CC.LastName LIKE '%s%'
like image 65
Damith Avatar answered Oct 19 '22 14:10

Damith