Back to all posts

SQL Server Data type

SQL Server provides a variety of data types to store different kinds of data efficiently. Here are some commonly used SQL Server data types: CHAR(size): A …


SQL Server provides a variety of data types to store different kinds of data efficiently. Here are some commonly used SQL Server data types:

CHAR(size): A FIXED length string (can contain letters, numbers, and special characters). The size parameter specifies the column length in characters - can be from 0 to 255 (2 bit). Default is 1

VARCHAR(size):A VARIABLE length string (can contain letters, numbers, and special characters). The size parameter specifies the maximum string length in characters - can be from 0 to 65535. Default is 1

BIT(size): Boolean data type, stores 0 for false and 1 for true. Converting to bit promotes any nonzero value to 1. The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.

int, bigint, smallint, and tinyint:

Data typeRangeRange expressionStorage
bigint-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
extremely large numbers or when dealing with identity columns that require a larger range
-2^63 to 2^63-18 Bytes
int-2,147,483,648 to 2,147,483,647
Suitable for most general-purpose integer storage needs.
-2^31 to 2^31-14 Bytes
smallint-32,768 to 32,767
Suitable for cases where the range of int is excessive or when storage space needs to be optimized.
-2^15 to 2^15-12 Bytes
tinyint0 to 255
It's particularly useful when space optimization is a concern or when dealing with binary data.
2^0-1 to 2^8-11 Byte

Integer constants greater than 2,147,483,647 are converted to the decimal data type, not the bigint data type. The following example shows that when the threshold value is exceeded, the data type of the result changes from an int to a decimal.

CSS
SELECT 2147483647 / 2 AS Result1, 2147483649 / 2 AS Result2 ;

Decimal and Numeric:

  • Precision and Scale: Both DECIMAL and NUMERIC require two parameters: precision (total number of digits) and scale (number of digits to the right of the decimal point).
  • Range: They can store values from -10^38 +1 to 10^38 -1.
  • Storage: Storage size depends on the precision and scale specified.
  • Usage: They are typically used for monetary values, quantities, or any numeric data where exact precision is required.

money and smallmoney:

Data typeRangeStorage
money-922,337,203,685,477.5808 to 922,337,203,685,477.5807 (-922,337,203,685,477.58
to 922,337,203,685,477.58 for Informatica. )
It is commonly used for storing large monetary values with up to four decimal places of precision.
8 bytes
smallmoney-214,748.3648 to 214,748.3647
It is used for storing smaller monetary values, typically with up to four decimal places of precision.
4 bytes

Float and Real:

FLOAT:Precision: The FLOAT data type is a floating-point number with a user-specified precision.
Storage: Requires 4 or 8 bytes of storage, depending on the precision specified.
Range: The range of values that can be stored in a FLOAT column is typically large, providing flexibility for scientific calculations or when dealing with extremely large or small numbers.
Usage: Suitable for cases where an approximation of real numbers is acceptable, such as scientific or engineering calculations. It's important to note that FLOAT is approximate and might not always represent exact values.
REAL:Precision: The REAL data type is a synonym for FLOAT(24).
Storage: Requires 4 bytes of storage.
Range: Similar to FLOAT, REAL provides a wide range of values.
Usage: Historically, REAL has been used for compatibility reasons with other systems or applications that expect this data type. However, it's recommended to use FLOAT instead for new development.
Data TypeDescriptionRangeStorageExampleTypical Usage
DATEStores date values without a time componentJanuary 1, 0001 CE to December 31, 9999 CE3 bytes'2024-05-04'Birthdates, event dates
TIMEStores time values without a date component00:00:00.0000000 through 23:59:59.99999993 to 5 bytes'14:30:00.000'Recording time durations, working hours
DATETIMEStores date and time values with millisecond precisionJanuary 1, 1753 CE to December 31, 9999 CE8 bytes'2024-05-04 14:30:00.000'Timestamps, scheduling
SMALLDATETIMEStores date and time values with minute precisionJanuary 1, 1900 CE to June 6, 2079 CE4 bytes'2024-05-04 14:30:00'Legacy systems, where minute precision is sufficient
DATETIME2Stores date and time values with high fractional seconds precisionJanuary 1, 0001 CE to December 31, 9999 CE6-8 bytes'2024-05-04 14:30:00.1234567'Scientific, financial applications requiring high precision
DATETIMEOFFSETStores date and time values with time zone informationSimilar to DATETIME210 bytes'2024-05-04 14:30:00.1234567 +05:30'Global or distributed systems, where time zone information is needed
Data typeOutput
time12:35:29.1234567
date2007-05-08
smalldatetime2007-05-08 12:35:00
datetime2007-05-08 12:35:29.123
datetime22007-05-08 12:35:29.1234567
datetimeoffset2007-05-08 12:35:29.1234567 +12:15

Keep building your data skillset

Explore more SQL, Python, analytics, and engineering tutorials.