Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 767
Prady Avatar asked Apr 19 '11 04:04

Prady


People also ask

How do I find the size of a field in SQL?

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.

How do I get the max value in a column in SQL?

The MAX() function returns the largest value of the selected column.

What is the maximum size of field in database?

65535 characters, excluding blobs.

How do I find the maximum row size in SQL?

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.


2 Answers

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)
like image 163
RichardTheKiwi Avatar answered Sep 23 '22 11:09

RichardTheKiwi


SELECT TOP 1 WITH TIES * 
FROM tbl
ORDER BY len(tbl.fieldA) DESC
like image 29
pmac72 Avatar answered Sep 26 '22 11:09

pmac72