SQL query for finding the longest name and shortest name in a table

后端 未结 29 2017
春和景丽
春和景丽 2021-01-31 00:11

I have a table with one of the columns is of type varchar(city). and want to find the longest and shortest of values stored in that column.

select a.city, a.city         


        
29条回答
  •  走了就别回头了
    2021-01-31 00:37

    select top(1) city, max(len(city)) [Length] from station group by city order by [Length]
    select top(1) city, max(len(city)) [Length] from station group by city order by [Length] DESC
    

    Tested in SQL Server 2016

提交回复
热议问题