how do i get column size

Oct 8, 2008 at 3:45 PM
Using the library i wrote a simple function to return the basic column information in a table.  I couldn't find the column size.  When a column is a character based datatype i want to list that as well.  What property of the column has that information?  Here is my function so you can see the pattern and goal.

function Get-SqlColumn2
    param($db, $tablename)
    ($db.Tables | where {$_.Name -eq $Tablename}).columns | Select Name, datatype, nullable | format-table -autosize

Oct 8, 2008 at 6:39 PM
Strangely enough there doesn't appear to be a property available in the SMO Column class which has length information, see the MSDN documentation for this class:

Looking at your function you can accomplish the same output with this (which doesn't have data length information either):
Get-SqlDatabase 'Z002\SqlExpress' AdventureWorks | Get-SqlTable -name "Person.Address" | Get-SqlColumn | Select Name, datatype, nullable | format-table -autosize

The Information_Schemas do have length and datatype information and you can use this function:
Get-SqlDatabase 'Z002\SqlExpress' AdventureWorks | Get-Information_Schema.Columns -tblname 'Address'
Feb 2, 2010 at 11:22 AM

look at the column.datatype object, they have the property MaximumLength which I think is what you looking for. I hope this helps :-)