Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Table info

Author  Topic 

dstalker
Starting Member

2 Posts

Posted - 2005-08-23 : 10:09:04
I want to create a select statement that will return to me useful information for a table. This info would include table name, column name, column type, column length, Max string length in column. The SQL statement I have so far errors on the Max string length.

select o.name as TableName,
c.name as ColName,
u.name as ColType,
c.length as ColLen,
(select max(LEN(LTRIM(RTRIM(c.name)))) from o.name) as maxlen

from sysobjects o inner join syscolumns c
on o.id = c.id
inner join systypes u
on c.xtype = u.xtype
where o.name='TableName'
group by o.name, c.name,u.name,c.length,c.colorder
order by c.colorder


Thanks. Dwayne

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-23 : 10:23:42
select * from information_schema.columns
where table_name='tt' order by ordinal_position


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

dstalker
Starting Member

2 Posts

Posted - 2005-08-23 : 11:10:59
Thanks for the response.
Maybe I should have said 'longest' instead of MAX. I want to change some column data types from Char(500) to Varchar(?), and I do not want to truncate any useful data, so I am curious if I can get all this in one SQL statement. This table does not seem to give me the actual string length i.e. (LTRIM(RTRIM(c.name)) Thanks.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-24 : 00:59:37
If you want to change from Char to varchar use the same column width

This will show you the maximum lengths of each column

select column_name,Data_type,Character_maximum_length from information_schema.columns
where table_name='tableName' order by ordinal_position

To know the maximum characters in the table

select name,length from syscolumns where id=object_id('tableName')
order by colid


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -