Bas Hendriks
Bas Hendriks

Reputation: 639

How can i check in MS SQL Server 2005 if a column is of a specific type

I want to change the type of a column in MS SQL Server 2005, but before i change the type of that column i want to check if that column is of the type i want to change. How can i do that in SQL?

Thanxs, Bas Hendriks.

Based on the anwser i wrote the following query that did the trick:

        IF NOT EXISTS (SELECT *
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = 'e_application'
            AND COLUMN_NAME = 'txt_locked_by'
            AND DATA_TYPE = 'nvarchar'
            AND CHARACTER_MAXIMUM_LENGTH = 15 )
        BEGIN
            ALTER TABLE.....
        END

Upvotes: 2

Views: 277

Answers (2)

nate wew
nate wew

Reputation: 189

you can use sp_help TableName , it also retrieve's the column's data types and other properties

Upvotes: 0

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171351

You can query the INFORMATION_SCHEMA tables.

E.g.

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTable'

Upvotes: 1

Related Questions