Either this is impossible, or I can't write a good internet search.
I have a query to get the table and column information for our database:
Select t.name [table], c.name [column]
From sys.tables t
Left join sys.columns c on t.object_id = c.object_id
What I'd like to do is add in something like
(Select AVG(LEN(c.name)) from t.name) [AVG LEN]
I'd obviously build in some checks to only run on varchar for Len, and regular AVG for numerics, but that part would be easy. It is getting SQL to actually treat the names correctly that I'm struggling with.
Best Answer
You can use dynamic SQL, a cursor and a global temporary table to collect this information. Note that performance won't be great (cursors are generally a bad idea) but if you only need to run this as a once-off then it should be fine.