SQL SERVER - 2005 - List All The Column With Specific Data Types

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

SQL SERVER � 2005 � List All The Column With Specific Data Types

August 9, 2007
Pinal Dave
SQL, SQL Server, SQL Tips and Tricks
65 Comments
Since we upgraded to SQL Server 2005 from SQL Server 2000, we have used following
script to find out columns with specific datatypes many times. It is very handy
small script.

SQL Server 2005 has new datatype of VARCHAR(MAX), we decided to change all our TEXT
datatype columns to VARCHAR(MAX). The reason to do that as TEXT datatype will be
deprecated in future version of SQL Server and VARCHAR(MAX) is superior to TEXT
datatype in features. We run following script to identify all the columns which are
TEXT datatype and developer converts them to VARCHAR(MAX)

Script 1 : Simple script to identify all the columns with datatype TEXT in specific
database
SELECT OBJECT_NAME(c.OBJECT_ID) TableName, c.name ColumnName
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
WHERE t.name = 'text' --you can change text to other datatypes
ORDER BY c.OBJECT_ID;
GO

Script 2 : Extended script to identify all the columns datatype and other
information in specific database
SELECT
OBJECT_NAME(c.OBJECT_ID) TableName
,c.name AS ColumnName
,SCHEMA_NAME(t.schema_id) AS SchemaName
,t.name AS TypeName
,t.is_user_defined
,t.is_assembly_type
,c.max_length
,c.PRECISION
,c.scale
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
ORDER BY c.OBJECT_ID;

You might also like