I am trying to get a list of all columns from a table with their data types, data lengths and the length of the longest value in that column.
I have this SQL for getting the columns and their data types and lengths:
SELECT
Object_Name(c.object_id),
c.name 'Column Name',
t.Name 'Data type',
c.max_length 'Max Length'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.system_type_id = t.system_type_id
WHERE
c.object_id = OBJECT_ID('MyTable')
And I have this SQL for getting the maximum length of a value:
SELECT Max(Len(MyColumn))
FROM MyTable
But I can’t figure out how to combine them. I am using SQL Server 2008.
Thanks for the suggestions. I have come up with the following solution. It gets me the data i need but would be interested to see if it can be made more efficient.