In SQL Server, there are several categories of data types, each designed to handle different kinds of data. Here’s an overview of the primary SQL Server data types:
1. Numeric Data Types
These are used to store numbers, both integer and decimal values.
int: Stores integers (whole numbers) from -2,147,483,648 to 2,147,483,647.bigint: Stores large integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.smallint: Stores smaller integers from -32,768 to 32,767.tinyint: Stores very small integers from 0 to 255.decimalornumeric: Stores fixed-point numbers with a specified precision and scale.- Syntax:
decimal(p, s)ornumeric(p, s) pis precision (total number of digits) andsis scale (number of digits to the right of the decimal point).- Example:
decimal(10, 2)can store values like12345678.90.
- Syntax:
float: Stores floating-point numbers. Typically used for scientific calculations with approximate precision.- Syntax:
float(n)wherendefines the precision (default is 53).
- Syntax:
real: Stores floating-point numbers with less precision thanfloat. Typically used for numbers requiring less precision.
2. Character and String Data Types
These store text and character data.
char: Fixed-length character data. Can store up to 8,000 characters.- Syntax:
char(n)wherenis the number of characters (1-8,000).
- Syntax:
varchar: Variable-length character data. Used to store strings of text.- Syntax:
varchar(n)wherenis the maximum number of characters (1-8,000). varchar(max)can store up to 2 GB of text.
- Syntax:
text: Deprecated in favor ofvarchar(max). Can store large amounts of text (up to 2 GB).nchar: Fixed-length Unicode character data.- Syntax:
nchar(n)wherenis the number of characters (1-4,000).
- Syntax:
nvarchar: Variable-length Unicode character data.- Syntax:
nvarchar(n)wherenis the maximum number of characters (1-4,000) ornvarchar(max)for up to 2 GB.
- Syntax:
ntext: Deprecated in favor ofnvarchar(max). Used to store large Unicode text (up to 2 GB).
3. Date and Time Data Types
These are used to store date, time, or both in SQL Server.
date: Stores only the date inYYYY-MM-DDformat.time: Stores only the time inHH:MI:SSformat.datetime: Stores date and time from 1753-9999 with a precision of 1/300th of a second.smalldatetime: Stores date and time from 1900-2079 with a precision of 1 minute.datetime2: Stores date and time from 0001-9999 with more fractional seconds precision (up to 7 digits).datetimeoffset: Stores date and time with an optional time zone offset.
4. Binary Data Types
These are used for storing binary data such as images, files, or encrypted values.
binary: Fixed-length binary data.- Syntax:
binary(n)wherenis the number of bytes (1-8,000).
- Syntax:
varbinary: Variable-length binary data.- Syntax:
varbinary(n)wherenis the maximum number of bytes (1-8,000). varbinary(max)can store up to 2 GB of binary data.
- Syntax:
image: Deprecated in favor ofvarbinary(max). Stores large binary data (up to 2 GB).
5. Other Data Types
bit: Stores Boolean data (0 or 1).uniqueidentifier: Stores globally unique identifiers (GUIDs).xml: Stores XML data in a structured format.cursor: Used for storing the reference to a cursor.table: Stores a result set for temporary use, often in the context of table variables.
6. Hierarchy and Spatial Data Types
These are used for hierarchical or geographical data.
hierarchyid: Represents a position in a hierarchy.geography: Stores geospatial data (longitude/latitude coordinates).geometry: Stores spatial data such as points, lines, and polygons, in a flat (Euclidean) coordinate system.
7. Money and Currency Types
These store monetary or currency data.
money: Stores monetary values with 4 digits of precision to the right of the decimal.smallmoney: Stores smaller monetary values with 4 digits of precision.
8. Other Specialized Data Types
Some additional types designed for specific use cases:
sql_variant: Can store any data type excepttext,ntext, andimage.timestamp(also calledrowversion): Stores a unique binary number that is automatically generated. Used to manage concurrency.datetime2anddatetimeoffsetare extensions for more flexible date-time storage.
Summary of Common Data Types:
| Data Type | Description |
|---|---|
int |
Integer type (whole numbers). |
bigint |
Larger integer type. |
decimal/numeric |
Fixed precision and scale numbers. |
varchar |
Variable-length string. |
char |
Fixed-length string. |
date |
Date only (no time). |
datetime |
Date and time with fixed precision. |
bit |
Boolean type (0 or 1). |
money |
Monetary values. |
uniqueidentifier |
GUID (Globally Unique Identifier). |
varbinary |
Variable-length binary data. |
xml |
XML data. |
SQL Server provides a wide variety of data types to cater to the diverse needs of applications, from handling simple integers to complex spatial data. It's important to choose the right type for your use case to ensure optimal performance and storage.
No comments:
Post a Comment