找到两列合并的最大长度

时间:2016-01-14 15:47:15

标签: sql sql-server

例如,我有一个带有FirstName和LastName列的Customers表 - 我假设必须连接这两列才能找到最长的客户名称。我该怎么做呢?

5 个答案:

答案 0 :(得分:4)

这将为您提供DB中最长的名称:

SELECT MAX(LEN(FirstName) + LEN(LastName)) AS MaxLen 
from Customers

答案 1 :(得分:2)

这将告诉您每个客户姓名的长度:

SELECT LEN(FirstName) + LEN(LastName) as TotalLength from Customers

答案 2 :(得分:2)

SELECT TOP 1 ID, FirstName, LastName, LEN(FirstName + LastName)
FROM Customers
ORDER BY LEN(FirstName + LastName) DESC;

ID是您的主键。这将为您提供最长连接名称的id,名字,姓氏和长度。

答案 3 :(得分:1)

SELECT * FROM names by order(length(first_name)+ length(last_name))desc limit 1;

答案 4 :(得分:0)

SELECT firstName, lastName 
FROM Customers t1
INNER JOIN (SELECT MAX(Len(firstName) + Len(lastName)) longest FROM Customers) t2
ON Len(t1.firstName) + Len(t1.lastName) = longest