SQL 如何获取表中字段使用的最大大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5711518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:11:53  来源:igfitidea点击:

how to get the max size used by a field in table

sqltsqlsql-server-2008

提问by Prady

I have a field which has been set to max size. How can i find the max size occupied by the field.

我有一个已设置为最大大小的字段。我如何找到该字段占用的最大大小。

For example if the records are for table TableA

例如,如果记录是表 TableA

FieldA

123
abcd
1234567

I need to know which row occupied the most size and what the size is

我需要知道哪一行占用的大小最多,大小是多少

Thanks

谢谢

Prady

普拉迪

回答by RichardTheKiwi

LENtests for the length in characters, e.g. "a" = 1 char

LEN测试以字符为单位的长度,例如 "a" = 1 个字符

select max(len(fieldA)) from tbl

DATALENGTHchecks for the size in bytes, an NVarchar occupies 2 bytes per character

DATALENGTH检查以字节为单位的大小,一个 NVarchar 每个字符占用 2 个字节

select max(datalength(fieldA)) from tbl

To get all the rows in the table that have the maximum length of data in FieldA,

要获取表中字段A中数据长度最大的所有行,

select *
from tbl join (select MAX(LEN(fieldA)) maxlen from tbl) l
    on l.maxlen = LEN(tbl.fieldA)

回答by pmac72

SELECT TOP 1 WITH TIES * 
FROM tbl
ORDER BY len(tbl.fieldA) DESC

回答by ShelS

You may to query this sql

你可以查询这个sql

Select Character_Maximum_Length
From INFORMATION_SCHEMA.COLUMNS
Where TABLE_CATALOG Like 'DatabaseName' And TABLE_NAME Like 'TableName' And COLUMN_NAME Like 'FieldName'