SQL Data Types | Data Types in SQL Server

post

In this tutorial, we’ll explore the different types of data available in SQL and understand why they are important.

Understanding Data Types and Their Importance

With the massive amount of data we work with daily, storing everything in a single type would make handling and organizing it very difficult. Data types help us manage different kinds of information efficiently.

Example:
When storing student data, we may need various data types—like using varchar for names, int for roll numbers, and date for the birthdate. This example highlights the importance of choosing the right data type when building a database.

Types of Data in SQL

1. Binary Data Types

These are used for storing binary data:

TypeDescription
binaryFixed-length binary data, up to 8000 bytes.
varbinaryVariable-length binary data, up to 8000 bytes.
varbinary(max)Variable-length binary data, up to 2^31 bytes.
imageLarge binary data, up to 2,147,483,647 bytes.

2. Exact Numeric Data Types

TypeDescriptionRange
bigintLarge integers-9,223,372,036,854,775,808 to 9,223,372,036,854,775,808
intStandard integers-2,147,483,648 to 2,147,483,648
smallintSmall integers-32,768 to 32,768
tinyintTiny range0 to 255
bitBoolean values0 or 1
decimalPrecise decimal numbers-10^38+1 to 10^38-1
numericSame as decimal-10^38+1 to 10^38-1
moneyMonetary values-922,337,203,685,477.5808 to 922,337,203,685,477.5808
smallmoneySmaller monetary values-214,748.3648 to 214,748.3648

3. Approximate Numeric Data Types

TypeDescriptionRange
floatFloating-point numbers-1.79E+308 to 1.79E+308
realApproximate real numbers-3.40E+38 to 3.40E+38

4. Character String Data Types

TypeDescription
charFixed-length characters (up to 8000 characters)
varcharVariable-length characters (up to 8000 characters)
varchar(max)Large variable-length text (up to 2^31 characters)
textVariable-length text (up to 2,147,483,647 characters)

5. Unicode Character String Data Types

TypeDescription
ncharFixed-length Unicode characters (up to 4000 characters)
nvarcharVariable-length Unicode characters (up to 4000 characters)
nvarchar(max)Large Unicode text (up to 2^31 characters)
ntextLarge Unicode text (up to 1 GB)

6. Date and Time Data Types

TypeDescriptionRange
datetimeDate and timeJan 1, 1753 to Dec 31, 9999
smalldatetimeSmaller range of date/timeJan 1, 1753 to Jun 6, 2079
dateOnly date valuesUnlimited
timeOnly time valuesUnlimited
yearYear values (2 or 4 digits)Unlimited
timestampTime-related unique valuesUnlimited

7. Miscellaneous Data Types

TypeDescription
clobLarge text data (up to 2 GB)
blobLarge binary objects
XMLStores XML formatted data
JSONStores JSON formatted data
cursorCursor objects used in loops/operations
UUIDUniversally unique identifiers
bfileBinary files stored externally

Summary

This tutorial gave you an overview of the different data types used in SQL. Commonly used ones include int, varchar, date, and boolean.

When defining data types, it's crucial to analyze the kind of data that will go into each column. Once set, only matching data types can be inserted successfully—otherwise, SQL will reject the command.

We’ll continue learning more in the upcoming tutorials.


Share This Job:

Write A Comment

    No Comments