Microsoft SQL Server supports a variety of data types to store different kinds of information. Here’s a summary of the most commonly used data types:
Numeric Data Types
INT
: Stores integers. Size: 4 bytes. Range: -2,147,483,648 to 2,147,483,647.BIGINT
: Stores larger integers. Size: 8 bytes. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.SMALLINT
: Stores smaller integers. Size: 2 bytes. Range: -32,768 to 32,767.TINYINT
: Stores very small integers. Size: 1 byte. Range: 0 to 255.DECIMAL(p,s)
: Stores fixed-point numbers with precisionp
and scales
. Size: Depends on precision. Precision: 1 to 38 digits.NUMERIC(p,s)
: Synonym forDECIMAL
. Same characteristics.FLOAT
: Stores floating-point numbers. Size: 4 bytes (float(24)) or 8 bytes (float(53)). Range: Approximate values with varying precision.REAL
: Stores floating-point numbers with less precision thanFLOAT
. Size: 4 bytes. Range: Approximate values with lower precision.
Date and Time Data Types
DATE
: Stores dates only. Size: 3 bytes. Range: January 1, 0001 to December 31, 9999.TIME
: Stores time of day. Size: 5 bytes. Range: 00:00:00.0000000 to 23:59:59.9999999.DATETIME
: Stores date and time. Size: 8 bytes. Range: January 1, 1753 to December 31, 9999, with 3.33 milliseconds precision.DATETIME2
: Stores date and time with more precision. Size: 6 to 8 bytes. Range: January 1, 0001 to December 31, 9999, with up to 100 nanoseconds precision.SMALLDATETIME
: Stores date and time with less precision. Size: 4 bytes. Range: January 1, 1900 to June 6, 2079, with minute precision.
Character and String Data Types
CHAR(n)
: Stores fixed-length non-Unicode characters. Size:n
bytes. Maximum length: 8,000 bytes.VARCHAR(n)
: Stores variable-length non-Unicode characters. Size: 1 to 8,000 bytes. Maximum length: 2 GB.TEXT
: Stores large non-Unicode text data. Size: Up to 2 GB. (Note:TEXT
is being deprecated in favor ofVARCHAR(MAX)
.)NCHAR(n)
: Stores fixed-length Unicode characters. Size:2 * n
bytes. Maximum length: 4,000 characters.NVARCHAR(n)
: Stores variable-length Unicode characters. Size: 2 bytes per character. Maximum length: 4,000 characters or 2 GB.NTEXT
: Stores large Unicode text data. Size: Up to 2 GB. (Note:NTEXT
is being deprecated in favor ofNVARCHAR(MAX)
.)
Binary Data Types
BINARY(n)
: Stores fixed-length binary data. Size:n
bytes. Maximum length: 8,000 bytes.VARBINARY(n)
: Stores variable-length binary data. Size: 1 to 8,000 bytes. Maximum length: 2 GB.IMAGE
: Stores large binary data. Size: Up to 2 GB. (Note:IMAGE
is being deprecated in favor ofVARBINARY(MAX)
.)
Other Data Types
BIT
: Stores a bit value. Size: 1 byte. Values: 0, 1, or NULL.XML
: Stores XML data. Size: 1 to 2 GB.UNIQUEIDENTIFIER
: Stores a globally unique identifier (GUID). Size: 16 bytes.
Special Data Types
TABLE
: Stores a result set for processing in SQL Server. Used primarily for table-valued parameters and variables.CURSOR
: Stores a reference to a cursor, which is a database object used to retrieve rows from a result set one at a time.SQL_VARIANT
: Stores values of various SQL Server-supported data types. Size: Varies.
Each data type has its use cases and constraints, so choosing the appropriate one is crucial for optimizing storage and performance.