How to Check if a Column Exists in a SQL Server Table?
Last Updated :
13 Sep, 2021
In this article, we will look at how to check if a particular column exists in a database table or not. For checking the existence of a column we need to create the table first. So, let us create a table with some columns and data.
Creating table:
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....);
Using the above syntax we will be creating a Student table. The student table will have three fields Name, Department, and Roll Number of a student. To create the table use the below query.
Query:
CREATE TABLE Student(Name varchar(40),
Department varchar(30),Roll_No int, );
Output:

This query will show the output as Commands completed successfully. It means that a student table is created in our database. To insert values in the table we have to use the INSERT query.
Insert Data into the Table:
Syntax:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
Using the above syntax we will be adding student data to our table. We have to insert the values according to the columns created. Use the below query to insert the data.
Query:
INSERT INTO Student
VALUES ('Rahul Sharma','Electronics',15),
('Soha Shaikh','Computer Science',24),
('Vivek Rao','Arts',31),
('Sonali Rane','Electronics',20);
Output:

We have added the data to our table. We can verify or print the data in the table using the SELECT query as below.
View Table Data:
Query:
SELECT * FROM Student
Output:

Checking Existence of the Column:
For checking the existence we need to use the COL_LENGTH() function.
Syntax:
COL_LENGTH ( 'table' , 'column' )
- COL_LENGTH() function returns the defined length of a column in bytes.
- We have to pass two parameters - table name and column name
- This function can be used with the IF ELSE condition to check if the column exists or not.
Now we use the below query to check the existence of a column.
Query:
IF COL_LENGTH('table_name','column_name') IS NOT NULL
PRINT 'Column Exists';
ELSE
PRINT 'Column does not Exists';
The above Student table has three columns Name, Department, and Roll Number. The below examples show how to check if a column exists in a database table.
Output 1:
Output 2:

Using COL_LENGTH() function we can find out if a column exists in our database table or not.
Similar Reads
How to Check a Column is Empty or Null in SQL Server In SQL Server table columns, there can be times when there is NULL data or Column Value is Empty (''). When data is displayed or used in data manipulations, there could be a need to remove these records with NULL values or Empty column values or replace the NULL value with another value like EMPTY v
5 min read
How to Check if a Row Already Exists in SQLite? SQLite is a widely used relational database management system due to its simplicity, lightweight design and energy efficiency features. When working with SQLite databases, It is very important to ensure data integrity therefore it is necessary to avoid accidentally inserting duplicated records. In t
3 min read
How to Check If a Row Already Exists in PL/SQL? In database operations, particularly when dealing with data insertion, it's essential to ensure that duplicate rows are not inadvertently added to a table. In PL/SQL (Procedural Language/Structured Query Language), developers often need to check whether a row already exists in a table before perform
3 min read
How to Check a Column is Empty or Null in MySQL? In the databases, determining whether a column is empty or null is a common task. MySQL provides various techniques to perform this check, allowing users to filter and manipulate data efficiently. This article delves into the methods for checking if a column is empty or null in MySQL, outlining the
4 min read
How to Add a Column with a Default Value to an Existing Table in SQL Server In SQL Server sometimes it may be required to set the default value to a Column in a table. This default value will be set as the column value when there is no value inserted or left empty for that particular column while data is inserted into the Table. Sometimes after creating a table, it may be r
8 min read
Check whether a Table exists in SQL Server database or not Before creating a table, it is always advisable to check whether the table exists in the SQL Server database or not. Checking for table existence before creation helps in avoiding duplication errors, ensures data integrity, and enables efficient database management. There are multiple methods in SQL
3 min read
Check the Dependencies of a Table in SQL Server Sometimes there is a need to find the dependencies of a table in SQL Server using SQL Server Management Studio or SQL Query. It is useful to have information about the dependencies while altering or dropping any table. In this article, we will learn how to find table dependencies in SQL Server using
2 min read
How to Drop a Table If It Exists? When working with databases we may need to remove a table that we no longer need. Dropping a table removes the table structure and all the data contained within it. However, we might want to ensure that we don't encounter errors if the table doesn't exist when trying to drop it. In this article, we'
4 min read
How to Get the Data Type of Columns in SQL Server? SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. SQL Server offers the SQL Server Management Studio which defines the database development and administration. In this article, we will learn how to retrieve the dat
4 min read
How to Check Column Types in PostgreSQL? In PostgreSQL, checking column types is an important aspect of understanding the structure and data stored in a database table. It helps database developers and administrators work effectively by providing a clear picture of the database schema.In this article, we will explore various methods to che
4 min read