查询提取名称的首字母

时间:2011-11-05 23:10:40

标签: sql

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

有人可以帮我修改这个,所以它可以得到“名字”的首字母。 (该表有Number,LastName,Birthday,Firstname)

3 个答案:

答案 0 :(得分:2)

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是您追求的功能

答案 1 :(得分:1)

在MySql中,您可以使用LEFT(Firstname, 1)SUBSTR(Firstname, 1, 1) 在MS-SQL中,您可以使用LEFT(Firstname, 1) 在Oracle中,您可以使用SUBSTR(Firstname, 1, 1)

答案 2 :(得分:0)

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