I need to find the max size of one value in a column. Like if we have column name with firstName . I need to know maximum number of bytes size of the first Name.
Asked
Active
Viewed 2,393 times
0
-
max allowed `firstName` size in column or max available `firstName` size in column? – demo Sep 16 '21 at 14:40
-
I am confident one of those duplicates is the answer to your question; which is unclear (hence why I have added both). – Thom A Sep 16 '21 at 15:02
2 Answers
1
If you are looking for the maximum length of a column, you can use the LEN
function which returns the length of a string and then use the MAX
function to return the maximum value in a set of values. Here is the sample.
SELECT MAX(LEN(column_name)) FROM table_name
If you want the maximum number of bytes, you can use the DATALENGTH
function which returns the number of bytes used to represent an expression. Note that this function counts both leading and trailing spaces when calculating the length of the expression.
SELECT MAX(DATALENGTH(column_name)) FROM table_name

Madhukar
- 1,194
- 1
- 13
- 29
0
If you want to find the length of longest first name in a table you can count the characters of each one and then take the max value.
For example:
select max(len(first_name)) from my_table
See running example at db<>fiddle.

The Impaler
- 45,731
- 9
- 39
- 76