MS SQL Server – Data Types

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:

  1. INT (Integer):
    • Stores whole numbers.
    • Range: -2,147,483,648 to 2,147,483,647.
  2. VARCHAR(n):
    • Variable-length character strings.
    • Maximum length is specified by ‘n’.
    • Example: VARCHAR(50) can store up to 50 characters.
  3. CHAR(n):
    • Fixed-length character strings.
    • Always reserves ‘n’ characters even if the string is shorter.
    • Example: CHAR(10) will always occupy 10 characters.
  4. 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.
  5. 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.
  6. DATE:
    • Stores date values in ‘YYYY-MM-DD’ format.
    • Example: ‘2023-09-29’.
  7. TIME:
    • Stores time values in ‘HH:MI:SS.nnnnnnn’ format.
    • Example: ’15:30:45.1234567′.
  8. DATETIME:
    • Stores date and time values in ‘YYYY-MM-DD HH:MI:SS.nnnnnnn’ format.
    • Example: ‘2023-09-29 15:30:45.1234567’.
  9. BOOLEAN:
    • Stores true/false or 1/0 values.
    • Commonly used for Boolean logic.
  10. BINARY(n):
    • Fixed-length binary data.
    • Stores binary data, such as images or documents.
    • Example: BINARY(16) for storing a 16-byte binary value.
  11. VARBINARY(n):
    • Variable-length binary data.
    • Similar to BINARY but allows variable-length storage.
  12. 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.
  13. TEXT and NTEXT:
    • Deprecated data types for storing large text data.
    • Replaced by VARCHAR(MAX) and NVARCHAR(MAX).
  14. UNIQUEIDENTIFIER:
    • Stores a globally unique identifier (GUID).
    • Often used as a primary key.
  15. XML:
    • Stores XML data.
    • Allows for querying and manipulating XML documents.
  16. JSON:
    • Introduced in newer SQL Server versions (e.g., SQL Server 2016 and later).
    • Stores JSON (JavaScript Object Notation) data.