SQL Server Data Types: The Complete Guide with Examples

sql server data types

SQL Server uses data types to define the kind of values in each column and variable. These data types help the database store and read values with speed and order.

Data types decide how much space a value takes and how SQL Server compares one value to another.

Categories of Data Types in SQL Server

SQL Server groups data types into main sets. Each one holds types that share a role.

Exact Number Types:

Numbers store whole or decimal numbers without error.

Here are examples:

  • int
  • bigint
  • decimal
  • numeric

You can use them like in the command below:

CREATE TABLE Sales(
  SaleID int,
  Quantity int,
  Price decimal(10,2)
);

This table stores sale records with exact counts and prices without loss.

Approximate Number Types:

Number types store numbers with floating point. Here is an example:

CREATE TABLE Measures(
  MeasureID int,
  Temperature float
);

It keeps measures with decimal values that may change over large ranges.

Date and Time Types:

This holds dates and times with combined values. Here is the example:

CREATE TABLE Events(
  EventID int,
  EventDate datetime2
);

It saves event dates with high precision for logs and reports.

Character Types:

Character types store text in fixed or variable length.

Example:

CREATE TABLE Users(
  UserID int,
  UserName varchar(50)
);

This stores user names with room for letters of different sizes.

Unicode Character Types:

Unicode Character types hold text in many language sets:

CREATE TABLE Books(
  BookID int,
  Title nvarchar(200)
);

This table stores book titles with characters from different languages.

Binary Types:

Binary types store raw bytes. For example:

CREATE TABLE Files(
  FileID int,
  FileData varbinary(max)
);

It saves files or images in a binary column.

Other Types:

These types cover unique values and spatial data for:

  • uniqueidentifier
  • xml
  • geometry
  • geography

For example:

CREATE TABLE Locations(
  LocationID uniqueidentifier,
  GeoData geography
);

This table stores unique IDs and spatial points for mapping.

A developer checks the range of values and the type of operation before choosing a data type.

Large ranges use bigint instead of int, text with multiple languages uses nvarchar instead of varchar, and large binary data uses varbinary(max) instead of binary.

Examples with SQL Queries

Use decimal for money:

CREATE TABLE Payments(
  PaymentID int,
  Amount decimal(12,2)
);
INSERT INTO Payments VALUES(1,125.75);
SELECT * FROM Payments;  

This query creates a table for payments with a decimal column to hold money values without rounding errors.

Store dates with datetime2:

CREATE TABLE Appointments(
  AppointmentID int,
  AppointmentDate datetime2
);
INSERT INTO Appointments VALUES(1,'2025-09-15 14:30:00');
SELECT * FROM Appointments;

This query stores an appointment date with high precision, which helps when events happen many times per day.

Use nvarchar for multilingual text:

CREATE TABLE User_comments(
  CommentID int,
  CommentText nvarchar(500)
);

INSERT INTO User_comments VALUES(1,N'مرحبا');
SELECT * FROM User_comments;

You can store any other language with nvarchar the type. So here the n that before varchar refers to “National Language”. It keeps full Unicode support for many scripts

Store binary files:

CREATE TABLE Attachments(
  AttachmentID int,
  FileData varbinary(max)
);

Here, it shows you how to store large binary data like images or documents inside a database column with the varbinary

Wrapping Up

You learned the role of SQL Server data types and how they define storage, comparison, and speed.

SQL Server has a lot of data types. Here is a quick list showing you them:

  • number
  • date
  • text
  • binary
  • special types.

Each type fits a certain use and can change how a database works.

FAQs

What are the main SQL Server data types?

SQL Server provides several categories:
  • Exact numeric: int, decimal, numeric
  • Approximate numeric: float, real
  • Character types: char, varchar, text
  • Date and time: date, datetime, time
  • Other: binary, varbinary, xml

How do you declare a data type in SQL Server?

Use CREATE TABLE or ALTER TABLE with column data types.

CREATE TABLE Employees (
   EmpID int,
   Name varchar(100),
   HireDate date
);

What is the difference between char and varchar in SQL Server?

  • char(n): Fixed length. Always uses n bytes.
  • varchar(n): Variable length. Uses only required space.

CREATE TABLE TestChar (
   FixedChar char(10),
   VarCharCol varchar(10)
);

Which SQL Server data type is best for storing money values?

Use decimal or money.
  • decimal(p,s): High precision. Recommended for accuracy.
  • money: Fixed precision. Easy but less flexible.

CREATE TABLE Products (
   ProductID int,
   Price decimal(10,2)
);

Similar Reads

SQL Server Bulk Insert: Storing a Million Records

In this article, you will learn how to insert more than a million records into the SQL Server database table.…

Mastering SQL Server: An Introduction to a Powerful Relational Database

This is an Introduction for SQL Server, which is a relational database management system (RDBMS). Which allows users to store,…

Table-Valued Parameters: Sending a Table to the Database

This tutorial will teach you how to store a big data table in C# or .NET into a database table…

How to Connect to SQL Server Step by Step

SQL Server keeps data safe and makes it easy to run requests and get answers fast. This guide shows how…

SQL Server Collation: Types and Configuration

The collation in SQL Server defines rules for sorting and comparing data. It covers default options, types, setup, checks, changes,…

SQL Server Architecture: Components & Functions

SQL Server has a set of parts that work as one system. Each part has a task and connects to…

How to Drop Database in SQL Server

DROP DATABASE in SQL Server lets you remove one or many databases. Understand the "drop database" and How It Works…

Previous Article

React Conditional Rendering Examples

Next Article

PHP array_key_last Function: How it Works with Examples

Write a Comment

Leave a Comment

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


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.