繁体   English   中英

在where子句中使用len

[英]using len in a where clause

我试图在数据集中找到字符数最多的城市。

但我收到此错误:

消息102,级别15,状态1,服务器WIN-ILO9GLLB9J0,第4行
'len'附近的语法不正确

我不确定为什么,因为当我用适当的数字替换max len(city)时似乎有效。

(我知道可以使用sort by来实现,但是我想理解为什么我无法使用where子句来实现这一点)

select city 
from station
where len(city) = max len(city)

表达方式

max len(city)

在语法上不正确。
您必须将其替换为具有最大长度的子查询:

select city 
from station
where len(city) = (select max(len(city)) from station)

我喜欢这样写:

select top (1) with ties city 
from station
order by len(city) desc;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM