wuling
wuling

Reputation: 1

Query to extract initial of name

SELECT t1.Number,  
       t1.LastName,  
       t1.Birthday,  
       t2.Number,  
       t2.LastName,  
       t2.Birthday 
FROM Employee t1, Employee t2 
WHERE t1.BirthDay = t2.Birthday
AND t1.EmpNo <> t2.EmpNo

Can someone help me to modify this, so it can get the initial of "Firstname". ( The table has Number,LastName, Birthday, Firstname)

Upvotes: 0

Views: 5023

Answers (3)

AFF
AFF

Reputation: 117

SELECT 
  t1.Number,
  t1.LastName,
  LEFT(t1.FirstName, 1) FirstInitialWithLeft,
  t1.Birthday,
  t2.Number,
  t2.LastName,
  SUBSTRING(t2.FirstName, 1, 1) SecondInitialWithSubstring,
  t2.Birthday
FROM Employee t1, Employee t2
WHERE t1.BirthDate = t2.Birthdate
  AND t1.EmpNo <> t2.EmpNo

Upvotes: 0

Colselaw
Colselaw

Reputation: 1079

SELECT t1.Number,
t1.LastName,
SUBSTRING(t1.FirstName, 1, 1) FirstInitial,
t1.Birthday,
t2.Number,
t2.LastName,
SUBSTRING(t2.FirstName, 1, 1) SecondInitial,
t2.Birthday
FROM Employee t1, Employee t2
WHERE t1.BirthDate = t2.Birthdate
AND t1.EmpNo <> t2.EmpNo

SUBSTRING is the function you're after

Upvotes: 2

Marco
Marco

Reputation: 57573

In MySql you can use LEFT(Firstname, 1) or SUBSTR(Firstname, 1, 1).
In MS-SQL you can use LEFT(Firstname, 1).
In Oracle you can use SUBSTR(Firstname, 1, 1)

Upvotes: 1

Related Questions