SQL选择Len(Col)Group by Len-等

时间:2018-01-16 02:55:05

标签: sql select

我想在我的数据库的名称列中找到最长的名称。按像素是理想的,但按字节数就可以了。这并不是很有效:

Select Name, Length(Name) as LEN
From AddressFile
Group by LEN, Name
Order by LEN DESC 

我希望得到类似的东西:

17, Johnson-Richardson
14, AnotherLongOne
11, Smithsonian
etc.

2 个答案:

答案 0 :(得分:1)

如果你真的想要字节,你可以使用:

SELECT name, DATALENGTH(name) AS LEN
FROM addressfile
ORDER BY LEN DESC

答案 1 :(得分:0)

你似乎想要:

Select Name, Length(Name) as LEN
From AddressFile
Order by LEN DESC ;

如果您只想要一个值,则可以添加fetch first 1 row only(或数据库的等效项)。