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

时间:2021-11-13 22:44:29

I am working in SQL data base. I have to make two columns. The first column is the length of the employee name(I have that) The second column is title of courtesy and then the last name of employee.

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

My problem is how do i add the case function to have Ms displayed as miss, mr. as mister and dr as doctor. I do not understand. Any help would be appreciated. Here is the code i have so far:

我的问题是如何添加案例功能让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 个解决方案

#1


1  

You can do something like this:

你可以这样做:

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

#2


0  

try this

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

#3


0  

Thanks all I fixed it I had to add the period after like mr.

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

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

#1


1  

You can do something like this:

你可以这样做:

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

#2


0  

try this

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

#3


0  

Thanks all I fixed it I had to add the period after like mr.

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

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