How to Get the Data Type of Columns in SQL Server?
Last Updated :
16 May, 2024
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 data type of columns in tables stored in our SQL Server databases.
How to Get the Data Type of Columns in SQL Server?
When working with SQL Server databases it is important to understand the data types of our columns for efficient data management and application development. There are 2 methods through which we can get the data type of columns in SQL Server are explained below
- Using INFORMATION_SCHEMA.COLUMNS View
- Using sys.columns View
Let's set up an environment
To understand how we can get the data type of columns in SQL Server, we will consider the table Customer as shown below:
CustomerID
| CustomerName
| City
| State
| Age
|
---|
1
| Amit Kumar
| Mumbai
| Maharashtra
| 28
|
2
| Kavya Sharma
| Delhi
| Delhi
| 35
|
3
| Amit Singh
| Bangalore
| Karnataka
| 42
|
4
| Anjali Gupta
| Kolkata
| West Bengal
| 30
|
1. Using INFORMATION_SCHEMA.COLUMNS View
- To get the data type of columns in SQL Server we can use the DATA_TYPE column from the INFORMATION_SCHEMA.COLUMNS view.
- This is a standard view provided by the SQL Server Management Studio which belongs to the INFORMATION_SCHEMA database which is a special kind of database that stores the metadata of the databases, tables, columns, and other objects stored in the system.
- Following is the syntax to use INFORMATION_SCHEMA.COLUMNS to get the data type of the columns in SQL Server.
Syntax:
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTableName';
Explanation: The syntax to get the data type of the columns is simple. We will select the COLUMN_NAME, DATA_TYPE field from the INFORMATION_SCHEMA.COLUMNS and then we will have to mention the name of the table whose data type of the columns is required.
Example: To get the data type of the columns of the Table Customer we will have to run the following query
Query:
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customers';
Output:
COLUMN_NAME
| DATA_TYPE
|
---|
CustomerID
| int
|
CustomerName
| varchar
|
City
| varchar
|
State
| varchar
|
Age
| int
|
Explanation: The following query returns an output table having two columns, where the first column denotes the name of the columns of the table customer and the second columns denotes the data type of the corresponding column.
2. Using sys.columns View
- In SQL server the sys.columns is a system catalog view that contains a row for each column of user defined tables.
- It provides metadata about each column in the table. We can query the sys.columns view to get the data type of each column present in our table.
- Following is the syntax to get the data type of columns using sys.columns in SQL server
Syntax:
SELECT COLUMN_NAME, TYPE_NAME(user_type_id) AS DATA_TYPE
FROM sys.columns
WHERE object_id = OBJECT_ID('YourTableName');
Explanation: To get the data type of the column provide the name of the table after the object id in the query and mention the name of the columns of your table in place of column_name in the query.
Example: To get the data type of the columns of the Table Customer we will have to run the following query
Query:
SELECT name, TYPE_NAME(user_type_id) AS DATA_TYPE
FROM sys.columns
WHERE object_id = OBJECT_ID('Customers');
Output:
Name
| DATA_TYPE
|
---|
CustomerID
| int
|
CustomerName
| varchar
|
City
| varchar
|
State
| varchar
|
Age
| int
|
Explanation: The following query returns an output table having two columns where the first column name denotes the name of the columns of the table customer and the second columns denotes the data type of the corresponding column from the table.
Conclusion
In conclusion, SQL Server provides efficient tools for database management, including the ability to retrieve column data types. Using either the INFORMATION_SCHEMA.COLUMNS view or the sys.columns catalog view, users can easily obtain the data type of columns in their SQL Server databases and helping effective database administration and development.
Similar Reads
How to Get the Type of Columns in SQL In SQL, the types of columns in a database table play a fundamental role in data management and query execution. Each column is designed to store specific types of data, such as numbers, text, dates, or binary data. Understanding these column types is essential for effective database design, query o
4 min read
How to Get the Data Type of a Columns in MariaDB When it comes to managing databases, understanding the types of data stored in each column is crucial. In MariaDB, this knowledge not only helps in organizing data efficiently but also enables more effective querying and analysis. In this article, we'll explore How to Get the Data Type of Columns in
4 min read
How to Get the Datatype of Table Columns in MySQL? When working with MySQL databases, knowing the datatype of table columns is essential to maintain data integrity and avoid errors. This guide covers methods to check column datatypes, such as using SHOW COLUMNS and querying INFORMATION_SCHEMA.COLUMNS. Understanding column datatypes helps in designin
4 min read
How to Retrieve Column Data Type in Oracle Using PL/SQL In Oracle databases, understanding column data types is essential for effective database management. PL/SQL provides a straightforward way to retrieve these data types, aiding in database design and query formulation. By querying system views like USER_TAB_COLUMNS, users can obtain valuable insights
4 min read
How to Convert DateTime to VarChar in SQL Server In SQL Server, date and time values are often stored in the DATETIME or DATE data types. However, there are situations where we may want to convert these values into a different format such as VARCHAR to display the date in a specific format or for further manipulation.In this article, we will explo
4 min read