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
FieldA
123
abcd
1234567
I need to know which row occupied the most size and what the size is
Thanks
Prady
Use COL_LENGTH() to Get a Column's Length in SQL Server In SQL Server, you can use the COL_LENGTH() function to get the length of a column. More specifically, the function returns the defined length of the column, in bytes. The function accepts two arguments: the table name, and the column name.
The MAX() function returns the largest value of the selected column.
65535 characters, excluding blobs.
MS SQL server allows only 8060 bytes of data max to be stored in a row. Hence your row size will always be <= 8060. But it is relaxed when a table contains varchar, nvarchar, varbinary, sql_variant, or CLR user-defined type colums.
LEN tests for the length in characters, e.g. "a" = 1 char
select max(len(fieldA)) from tbl
DATALENGTH checks for the size in bytes, an NVarchar occupies 2 bytes per character
select max(datalength(fieldA)) from tbl
To get all the rows in the table that have the maximum length of data in FieldA,
select *
from tbl join (select MAX(LEN(fieldA)) maxlen from tbl) l
on l.maxlen = LEN(tbl.fieldA)
SELECT TOP 1 WITH TIES *
FROM tbl
ORDER BY len(tbl.fieldA) DESC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With