简体   繁体   English

如何 select 并按字符对最大列大小的记录进行排序?

[英]How to select and sort records with biggest column size by characters?

Table Customers客户

ID    NAME LETTER
001   TOM  Lorem Ipsum
002   JEK  Lorem Ipsum
003   MAX  texttexttexttext...(30k letters)
004   JIZ  NULL
005   ZAK  texttexttexttext...(50k letters)

The 'Letter' data type in DB is 'text', I have some records that contain 50k+ letters even my microsoft sql client wont load the full size of it:/ DB 中的“字母”数据类型是“文本”,我有一些包含 50k+ 字母的记录,即使我的 microsoft sql 客户端也无法加载它的完整大小:/

Anyways I need to select IDS that have the biggest lenght by symbols at letter column无论如何,我需要 select IDS 在字母列中具有最大长度的符号

I tried next one: SELECT TOP 100 * FROM Customers ORDER BY CHAR_LENGTH(Letter);我尝试了下一个:SELECT TOP 100 * FROM Customers ORDER BY CHAR_LENGTH(Letter);

but looks like my db/sql client dsnt have that function:/ also I tried len(Letter) but argument data type is invalid for len function(但看起来我的 db/sql 客户端 dsnt 有 function:/ 我也试过 len(Letter) 但参数数据类型对 len 函数无效(

If you want the actual number of characters in the text field, cast it to a varchar , that should work for most scenarios:如果您想要文本字段中的实际字符数,请将其转换为varchar ,这应该适用于大多数情况:

select top(100) *
from Customers
order by len(cast(letter as varchar(max))) desc

If you want bytes, text can be used with datalength如果你想要字节, text可以与datalength一起使用

select top(100) *
from Customers
order by datalength(letter) desc

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

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