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 Find the Maximum of Multiple Columns in SQL Server?
When working with SQL Server databases, there are times when we need to find the maximum value among multiple columns. This task can be accomplished using various techniques within SQL queries. By using functions like CASE and GREATEST, SQL Server provides efficient ways to determine the maximum val
4 min read
How to Retrieving 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 Sorting Data According to More Than One Column in SQL Server
Sorting data is the basic operation of a database environment. There is the SQL Server which is one of the most common relational database management systems that has a very powerful sorting function based on one or more columns. Though sorting within one column is straightforward, sorting data acco
5 min read
How to Show all Columns in the SQLite Database using Python ?
In this article, we will discuss how we can show all columns of a table in the SQLite database from Python using the sqlite3 module. Approach:Connect to a database using the connect() method.Create a cursor object and use that cursor object created to execute queries in order to create a table and i
3 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 ch
4 min read
Convert the data type of Pandas column to int
In this article, we are going to see how to convert a Pandas column to int. Once a pandas.DataFrame is created using external data, systematically numeric columns are taken to as data type objects instead of int or float, creating numeric tasks not possible. We will pass any Python, Numpy, or Pandas
2 min read
How to Check if a Column Exists in a SQL Server Table?
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,
2 min read