Data types in Microsoft SQL Server define the kind of data that can be stored in a column or variable within a database. Each data type has specific properties and limitations. Here are some common SQL Server data types:
- INT (Integer):
- Stores whole numbers.
- Range: -2,147,483,648 to 2,147,483,647.
- VARCHAR(n):
- Variable-length character strings.
- Maximum length is specified by ‘n’.
- Example: VARCHAR(50) can store up to 50 characters.
- CHAR(n):
- Fixed-length character strings.
- Always reserves ‘n’ characters even if the string is shorter.
- Example: CHAR(10) will always occupy 10 characters.
- DECIMAL(p, s):
- Exact numeric data type for storing fixed-point numbers.
- ‘p’ represents the total number of digits, and ‘s’ is the number of decimal places.
- Example: DECIMAL(10, 2) can store numbers like 12345.67.
- FLOAT(n):
- Approximate numeric data type for storing floating-point numbers.
- ‘n’ specifies the number of bits used to store the mantissa.
- Example: FLOAT(24) for single-precision, FLOAT(53) for double-precision.
- DATE:
- Stores date values in ‘YYYY-MM-DD’ format.
- Example: ‘2023-09-29’.
- TIME:
- Stores time values in ‘HH:MI:SS.nnnnnnn’ format.
- Example: ’15:30:45.1234567′.
- DATETIME:
- Stores date and time values in ‘YYYY-MM-DD HH:MI:SS.nnnnnnn’ format.
- Example: ‘2023-09-29 15:30:45.1234567’.
- BOOLEAN:
- Stores true/false or 1/0 values.
- Commonly used for Boolean logic.
- BINARY(n):
- Fixed-length binary data.
- Stores binary data, such as images or documents.
- Example: BINARY(16) for storing a 16-byte binary value.
- VARBINARY(n):
- Variable-length binary data.
- Similar to BINARY but allows variable-length storage.
- VARCHAR(MAX) and NVARCHAR(MAX):
- Variable-length character strings with maximum storage capacity.
- Suitable for storing large text or binary data.
- VARCHAR(MAX) for non-Unicode character data, and NVARCHAR(MAX) for Unicode character data.
- TEXT and NTEXT:
- Deprecated data types for storing large text data.
- Replaced by VARCHAR(MAX) and NVARCHAR(MAX).
- UNIQUEIDENTIFIER:
- Stores a globally unique identifier (GUID).
- Often used as a primary key.
- XML:
- Stores XML data.
- Allows for querying and manipulating XML documents.
- JSON:
- Introduced in newer SQL Server versions (e.g., SQL Server 2016 and later).
- Stores JSON (JavaScript Object Notation) data.