x

Select all employees which first names start with the same letter

Select all employees which first names start with the same letter?

more ▼

asked Sep 12 at 02:11 AM in Default

avatar image

aj2017
0 1

(comments are locked)
10|1200 characters needed characters left

4 answers: sort voted first

I would advise you research the following areas

SELECT

FROM

WHERE and

LIKE

Take a go your self and let me know if you have any issues. Good luck!

more ▼

answered Sep 12 at 07:23 AM

avatar image

sp_lock
10.8k 27 37 37

(comments are locked)
10|1200 characters needed characters left

A few different ways to approach this but the following comes right to mind (the substring function is going to force scans).

It finds any first initials that are used more than once (`having count(*)>1`), then pulls all the names that begin with any of those initials. You could easily pull the subquery into a CTE and join against it, use row_number(), etc...

 select name 
 from Employee 
 where substring(name, 1, 1) in (
         select substring(name, 1, 1) as firstInitial 
         from Employee
         group by substring(name, 1, 1)
         having count(*) > 1
     )
more ▼

answered Sep 13 at 02:11 AM

avatar image

KenJ
25k 3 10 20

@KenJ. Brilliant!! Thank you so much.

Sep 13 at 06:15 AM aj2017
(comments are locked)
10|1200 characters needed characters left

Anything like that?

select a.name, b.name from emp as a, emp as b where SUBSTRING(a.name,1,1) = SUBSTRING(b.name,1,1) and a.id < b.id

more ▼

answered Sep 12 at 01:37 PM

avatar image

aj2017
0 1

(comments are locked)
10|1200 characters needed characters left

No at all, as this will fetch all names starting with "B".

I have to pull all the Names STARTING with the SAME letter (first letter must be the same - it can be ANY letter, not a specific one)

more ▼

answered Sep 13 at 01:23 AM

avatar image

aj2017
0 1

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x57

asked: Sep 12 at 02:11 AM

Seen: 40 times

Last Updated: Sep 13 at 06:15 AM

Copyright 2017 Redgate Software. Privacy Policy