Skip to content

SQL Query to Get Column Type

Published: at 08:52 PM

In relational databases, understanding the data types and properties of columns within a table is crucial for data manipulation and analysis. This article delves into a powerful SQL query that allows you to retrieve comprehensive metadata about columns in a specific table.

The Query:

SELECT
  c.name 'Column Name',
  t.Name 'Data type',
  c.max_length 'Max Length',
  c.precision,
  c.scale,
  c.is_nullable,
  ISNULL(i.is_primary_key, 0) 'Primary Key'
FROM
  sys.columns c
INNER JOIN
  sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN
  sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
  sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
  c.object_id = OBJECT_ID('<SchemaName>.<TableName>');

Explanation:

Benefits of Using This Query:

Implementation and Customization:

Replace '<SchemaName>.<TableName>' with the actual name of the table you want to introspect. This query can be easily integrated into your SQL development environment or directly executed within your database management system.


Previous Post
AWS Windows Server
Next Post
Fix for Error - "Unable to access the IIS metabase."