How to check if the Column exists in a SQL Server Table

To check if the Column exists in a SQL Server Table, run the following code that checks the INFORMATION_SCHEMA.COLUMNS view:

IF EXISTS(
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'Schema_name'
AND TABLE_NAME='Table_Name'
AND COLUMN_NAME ='Column_Name')
BEGIN
-- If the column exists, the code in this block will run.
PRINT 'The column exists!'
END

check_column_information_schema_columns

To check if the Column exists, you can also use COL_LENGTH function that returns the defined column length, in bytes. COL_LENGTH command will return NULL if the column does not exist, on an error or if you don't have permission to view the object:

IF COL_LENGTH('Table_Name','Column_Name') IS NOT NULL
BEGIN
-- If the column exists, the code in this block will run.
PRINT 'The column exists!'
END

check_column_col_length

Read related articles:
How to check if the View exists in a SQL Server Database
How to check if the Table exists in a SQL Server Database

How to List all Columns in a SQL Server Table
List all Nullable or Non-Nullable Columns in a SQL Server Table

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">