Showing posts with label date types in SQL Server. Show all posts
Showing posts with label date types in SQL Server. Show all posts

Working with Date, String, and Numeric Types in SQL Server - Beginner Tutorial

Part 7: Working with Date, String, and Numeric Types in SQL Server

Microsoft SQL Server Tutorial Series: Beginner to Expert


Welcome to Part 7 of our SQL Server tutorial series! In this lesson, we’ll dive into the essential data types that SQL Server offers for handling dates, strings, and numbers — the building blocks for storing and manipulating your data.


By the end of this article, you’ll understand:

  • How to work with date and time data types
  • The different string data types and their uses
  • Numeric types and how to choose the right one for your data
  • Examples of working with these data types in SQL queries

📅 Working with Date and Time Data Types

SQL Server provides several data types to store dates and times. Choosing the right one depends on the precision and storage size you need.

Data Type Description Storage Size Example Format
DATE Stores only date (year, month, day) 3 bytes YYYY-MM-DD (e.g., 2025-09-09)
TIME Stores time of day (hour, minute, second, fractional seconds) 3-5 bytes HH:MM:SS[.fractional seconds]
DATETIME Stores date and time with 3.33 ms precision 8 bytes YYYY-MM-DD HH:MM:SS
DATETIME2 Stores date and time with higher precision 6-8 bytes YYYY-MM-DD HH:MM:SS[.fractional seconds]
SMALLDATETIME Stores date and time with less precision (1 minute) 4 bytes YYYY-MM-DD HH:MM:00

Example: Creating a table with date and time columns

CREATE TABLE Events (
    EventID INT PRIMARY KEY,
    EventName VARCHAR(100),
    EventDate DATE,
    StartTime TIME,
    CreatedAt DATETIME2 DEFAULT SYSDATETIME()
);

📝 String Data Types in SQL Server

Strings are used to store text. SQL Server supports different string types depending on length and Unicode support:

Data Type Description Max Length Use Case
CHAR(n) Fixed-length non-Unicode string 1 to 8000 When string length is consistent
VARCHAR(n) Variable-length non-Unicode string 1 to 8000 Most common for English or ASCII text
VARCHAR(MAX) Variable-length non-Unicode string (max size) Up to 2 GB Very long text, e.g., descriptions, documents
NCHAR(n) Fixed-length Unicode string 1 to 4000 International characters with fixed length
NVARCHAR(n) Variable-length Unicode string 1 to 4000 Supports international/multilingual text
NVARCHAR(MAX) Variable-length Unicode string (max size) Up to 2 GB Long international/multilingual text

Example: Creating a table with string columns

CREATE TABLE Users (
    UserID INT PRIMARY KEY,
    UserName VARCHAR(50),
    Email NVARCHAR(100),
    Bio VARCHAR(MAX) NULL
);

🔢 Numeric Data Types in SQL Server

Numbers in SQL Server come in various sizes and types. Here are the main numeric types:

Data Type Description Storage Size Example Values
INT Integer (whole number) 4 bytes -2,147,483,648 to 2,147,483,647
SMALLINT Small integer 2 bytes -32,768 to 32,767
TINYINT Very small integer 1 byte 0 to 255
BIGINT Large integer 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
DECIMAL(p,s) Fixed precision and scale decimal number 5 to 17 bytes e.g., DECIMAL(10,2) = 12345678.90
FLOAT Approximate floating point number 4 or 8 bytes 3.14159, 2.7e10

Example: Creating a table with numeric columns

CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(100),
    Price DECIMAL(10, 2),
    Quantity INT,
    Weight FLOAT
);

💡 Tips for Choosing Data Types

  • Use DATE if you only need the date, not time.
  • Choose Unicode types like NVARCHAR if your app needs to support multiple languages.
  • Use DECIMAL for exact numeric values like money; use FLOAT for approximate values.
  • Use the smallest integer type that fits your range to save space.

📋 Quick Data Types Cheat Sheet

Dates: DATE, DATETIME, DATETIME2
Strings: VARCHAR, NVARCHAR, CHAR, NCHAR
Numbers: INT, SMALLINT, TINYINT, BIGINT, DECIMAL, FLOAT

🔍 Summary

Understanding SQL Server’s date, string, and numeric data types is fundamental to designing efficient databases and writing effective queries. By choosing the right data type, you optimize storage and ensure your data behaves as expected.

In the next part of this series, we’ll explore advanced SQL functions and how to manipulate data more powerfully.


Did you find this article helpful?

Feel free to leave a comment or share your questions below!


Featured Post

GROUP BY, HAVING, and Aggregations in SQL Server Explained

Part 9: GROUP BY, HAVING, and Aggregations in SQL Server Microsoft SQL Server Tutorial Series: Beginner to Expert Welcome to Part 9 of...

Popular Posts