juan
juan

Reputation: 1

List the employees whose last names begin with an "L"

In SQL Server Management Studio

List the employees whose last names begin with an "L". Both first name and last name appear in the same column called employeename and the last name is the second word.

This is what I came up with but only works for first name since is the first word:

SELECT employeename
FROM employee_T 
WHERE employeename LIKE 'L%' 

So what can I add to make it work?

Upvotes: 0

Views: 3335

Answers (2)

Gilad Green
Gilad Green

Reputation: 37299

You can add a ' ' before the 'L%' meaning that it is after a previous words:

select employeename 
from employee_T 
where employeename LIKE ' L%'

This option won't work if you have records with more than 2 words

Or use substring by last index of ' ' and do Like on that

Or use this to get the last part of your name and compare to what you want:

select employeename 
from employee_T 
where SUBSTRING_INDEX(employeename, ' ', -1) LIKE 'L%'

Upvotes: 4

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521674

Assuming every entry you want to match will look like this:

FirstName LastName

then all you need to do is add a space to your LIKE condition:

SELECT employeename
FROM employee_T
WHERE employeename LIKE ' L%'

If some of your data might only have a single name, or three names, then all bets are off for the above.

Upvotes: 1

Related Questions