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
Following query seems simple enough:
select
city, leng
from
(select top 1
city, len(city) leng
from
station
where
len(city) = (select min(len(city)) from station)
order by
city
Union all
select top 1
city, len(city) leng
from
station
where
len(city) = (select max(len(city)) from station)
order by
city) result;
The 1st query inside returns the city with the minimum length, while the 2nd query returns the city with the maximum length.
Hope this helps.