Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
SQL

How to check column data types of a table in SQL

- 12.10.21 | 15.02.26 - ErcanOPAK

The Scenario: You are handed a legacy database with hundreds of tables, or perhaps you’re debugging a “Type Mismatch” error in your backend. You need to know exactly what a table is made of—data types, nullability, and precision—without clicking through a clunky UI.

The Solution: While every database engine has its own way, the INFORMATION_SCHEMA is the universal, ISO-standard approach that works across SQL Server, MySQL, and PostgreSQL.


1. The Universal Approach (SQL Server, MySQL, Postgres)

The INFORMATION_SCHEMA.COLUMNS view is a treasure trove of metadata. Use this query to get a comprehensive “DNA report” of your table structure:

SELECT 
    COLUMN_NAME,
    DATA_TYPE,
    IS_NULLABLE,
    CHARACTER_MAXIMUM_LENGTH AS MaxLength,
    NUMERIC_PRECISION AS Precision,
    NUMERIC_SCALE AS Scale
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTableName' 
ORDER BY ORDINAL_POSITION;

Note: If you have multiple schemas (like dbo and sales), don’t forget to add AND TABLE_SCHEMA = 'dbo' to your WHERE clause!

2. The T-SQL “Pro” Way (SQL Server Specific)

If you are strictly using SQL Server, there is a built-in stored procedure that provides a beautifully formatted report including indexes and constraints with a single command:

EXEC sp_help 'YourTableName';

Alternatively, for a more modern and queryable approach in SQL Server, you can use the system catalog views:

SELECT 
    c.name AS 'ColumnName',
    t.name AS 'DataType',
    c.max_length AS 'Size',
    c.is_nullable AS 'IsNullable'
FROM sys.columns c
JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE c.object_id = OBJECT_ID('YourTableName');

🔍 Quick Guide to the Results:

  • CHARACTER_MAXIMUM_LENGTH: Crucial for VARCHAR fields. If it’s -1, it means MAX.
  • NUMERIC_PRECISION: The total number of digits in a DECIMAL or NUMERIC field.
  • NUMERIC_SCALE: The number of digits to the right of the decimal point.
  • IS_NULLABLE: Tells you if your code needs to handle null checks for this field.

Summary

Understanding your schema is the first step to writing efficient queries. Use INFORMATION_SCHEMA for portability, or sp_help for a quick, deep dive in SQL Server environments.

Related posts:

SQL Server Indexing Secret: How Covered Indexes Can Make Queries 100x Faster

Update Column with row_number() in SQL

SQL Indexing: Why Your Query Ignores Indexes and Scans 10 Million Rows Instead

Post Views: 65

Post navigation

Split and Join in Javascript
How to use SQL RAISEERROR() messages in C#

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (448)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com