如何在 SQL Server 中组合名字、中间名和姓氏 [英] How to combine first name, middle name and last name in SQL server

查看:90
本文介绍了如何在 SQL Server 中组合名字、中间名和姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以参考以下查询以获得相同的结果-

you can refer the below queries to get the same-

select FirstName +' '+ MiddleName +' ' + Lastname as Name from TableName.

2

select CONCAT(FirstName , ' ' , MiddleName , ' ' , Lastname) as Name from 
  TableName

3

select Isnull(FirstName,' ') +' '+ Isnull(MiddleName,' ')+' '+ Isnull(Lastname,' ') 
from TableName.

注意:Point 1 查询返回,如果所有列都有某个值,如果任何人为 null 或为空,那么它将为所有返回 null,意味着 Name 将返回NULL";价值.

Note: Point 1 query return if all columns have some value if anyone is null or empty then it will return null for all, means Name will return "NULL" value.

要避开点号 1,可以使用点号 2 或点号 3 -

To avoid the point number 1, you can use point number 2 or point number 3 -

我们可以使用 IsNullCONCAT 关键字来达到相同的效果.

We can use IsNull or CONCAT keyword to get the same.

如果任何人包含空值,则' '(空格)将与下一个值相加.

If anyone containing null value then ' ' (blank space) will add with next value.

推荐答案

  • 使用 RTRIMLTRIM 的组合将去除每一端的任何空白.
  • CONCAT 将名称段附加在一起
  • COALESCENULL 替换为空字符串
    • Using a combination of RTRIM and LTRIM will strip any white-space on each end.
    • CONCAT to append the name segments together
    • COALESCE to replace NULL with an empty string
    • 格式化以提高可读性

      SELECT 
          RTRIM(LTRIM(
              CONCAT(
                  COALESCE(FirstName + ' ', '')
                  , COALESCE(MiddleName + ' ', '')
                  , COALESCE(Lastname, '')
              )
          )) AS Name
      FROM TableName
      

      这篇关于如何在 SQL Server 中组合名字、中间名和姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆