有人可以帮我理解案例功能吗?

时间:2015-04-14 18:25:03

标签: sql-server

我在SQL数据库中工作。我必须做两个专栏。第一列是员工姓名的长度(我有)第二列是礼貌的标题,然后是员工的姓氏。

我的问题是我如何添加案例功能让Ms显示为miss,先生。先生和医生博士。我不明白。任何帮助,将不胜感激。这是我到目前为止的代码:

SELECT 'Your full name is ' + CAST(LEN(FirstName) + LEN(LastName) AS VARCHAR(12)) + ' character(s).' AS [Length of Employee Name]
       , TitleOfCourtesy + LastName AS Title
  FROM dbo.Employees

3 个答案:

答案 0 :(得分:1)

您可以这样做:

select
  ... your previous things here ...
  case TitleOfCourtesy 
    when 'Mr' then 'Mister '
    when 'Ms' then 'Miss '
    when 'Dr' then 'Doctor '
    else '' end + LastName as Title

答案 1 :(得分:0)

试试这个

SELECT  'Your full name is ' + CAST(LEN(FirstName) + LEN(LastName) AS VARCHAR(12)) + ' character(s).' AS [Length of Employee Name], 
CASE 
    WHEN TitleOfCourtesy = 'Ms' THEN 'Miss '
    WHEN TitleOfCourtesy = 'Mr' THEN 'Mister '
    WHEN TitleOfCourtesy = 'dr' THEN 'Doctor ' END + LastName AS Title
FROM  dbo.Employees

答案 2 :(得分:0)

谢谢我修好的所有内容,我必须像先生一样添加句号。

SELECT        'Your full name is ' + CAST(LEN(FirstName) + LEN(LastName) AS VARCHAR(12)) + ' character(s).' AS [Length of Employee Name]
    ,   CASE TitleOfCourtesy 
            WHEN 'Mr.' THEN 'Mister ' 
            WHEN 'mrs. ' THEN 'Miss' 
            WHEN 'Ms.' THEN 'Miss ' 
            WHEN 'Dr.' THEN 'Doctor ' 
            ELSE '' END 
            + TitleOfCourtesy + LastName
            AS title
FROM            dbo.Employees