You are on page 1of 8

Data Descriptions Lower Range Upper Range Storage

Type

bit It is an integer type that allows 0 1 1 byte


us to store 0, 1, and NULL values.

tinyint It allows us to store whole 0 255 1 byte


numbers from 0 to 255.

smallint It allows us to store whole -2^15 (-32,768) 2^15-1 (32,767) 2 byte


numbers between -32768 to
32767.

int It allows to store whole numbers -2^31 (-2,147,483,648) 2^31-1 (2,147,483,647) 4 byte
between -2,147,483,648 and
2,147,483,647

bigint It allows to store whole numbers -2^63 (- to 2^63-1 8 byte


between - 9,223,372,036,854,775,808) (9,223,372,036,854,775,807)
9,223,372,036,854,775,808 and
9,223,372,036,854,775,807

Decimal(p, It is used for scale and fixed ?10^38+1 10^381?1 5 to 17


s) precision numbers. bytes
The p parameter indicates the
maximum total number of digits
that can be stored (both to the
left and the right of the decimal
point). By default, it is 18 but can
be in a range of 1 to 38.
The s parameter indicates the
maximum number of digits
stored to the right of the decimal
point. By default, its value is 0
but can be from 0 to p.

Numeric(p, It is similar to the decimal data ?10^38+1 10^38?1 5 to 17


s) type because both are bytes.
synonyms.

smallmoney It allows storing monetary or -214,748.3648 214,748.3647 4 byte


currency values.

money It allows to store monetary or -922,337,203,685,477.5808 922,337,203,685,477.5807 8 byte


currency values.

Approximate numeric data types


This data type is used to store floating-point and real values. It is mainly used in scientific calculations.

Data Type Lower range Upper Range Storage Precision

float(n) ?1.79E+308 1.79E+308 It depends on n. 7 digit


4 or 8 bytes
real ?3.40E+38 3.40E+38 4 byte 15 digit

Date and Time data types


We use these data types to hold the temporal values such as date and time, including time offset in a column. We can categories this
data type into six types, which are summarized in below table:

Data Type Descriptions Lower Range Upper Range Storage

date It is used to store dates in SQL Server. By default, 0001-01-01 9999-12-31 3 bytes
its format is YYYY-MM-DD, and its value is 1900-
01-01.

datetime It is used to store date and time with fractional 1753-01-01 9999-12-31 8 bytes
seconds. Its default value is 1900-01-01 00:00:00.
Its accuracy is in the increment of .000, .003, or
.007 seconds. It is recommended to avoid this
data type and use datetime2 instead.

datetime2 It specifies date and time with fractional seconds 0001-01-01 9999-12-31 6 to 8
and an accuracy of 100 nanoseconds. It provides 00:00:00 23:59:59.9999999 bytes
precision from 0 to 7 digits. By default, its
precision is 7, and the format is YYYY-MM-DD
hh:mm: ss[.fractional seconds].
datetimeoffset It is the same as datetime2 with the addition of 0001-01-01 9999-12-31 10 bytes
a time zone offset. Its timezone offset value is 00:00:00 23:59:59.9999999
between -14:00 through +14:00.

smalldatetime It specifies a date along with the time of day and 1900-01-01 2079-06-06 4 bytes
an accuracy of 1 minute. Here time is calculated 00:00:00 23:59:59
on a 24-hour clock, with seconds starting at zero
(:00) and no fractional seconds.

time It specifies time data only with an accuracy of 00:00:00.0000000 23:59:59.9999999 3 to 5


100 nanoseconds. It is based on a 24-hour clock bytes
without time zone awareness. By default, its
format is hh:mm:ss[.nnnnnnn].

Character string data type


This data type allows us to define the character data type only, which can be fixed or variable in length. The following table illustrates
all character string data type support in SQL Server:

Data Type Descriptions Lower Upper Range Storage


Range

char(n) It is used to store fixed-length non-Unicode character 0 8000 characters n bytes


data. characters

varchar(n) It is used to store variable-length non-Unicode character 0 8000 characters n bytes + 2


data. characters bytes
varchar(max) It is a data type that stores variable-length data. It is 0 2^31 n bytes + 2
recommended to avoid this data type unless required characters characters bytes
because of its huge memory storage.

text It is a variable-length character string. It is also 0 2,147,483,647 n bytes + 4


recommended to avoid this data type because it would characters chars bytes
be deprecated in future releases.

Unicode character string data types


This data type allows us to define the full range of Unicode character sets encoded in the UTF-16 character set. The following table
illustrates all Unicode character string data type support in SQL Server:

Data Type Descriptions Lower Upper Range Storage


Range

nchar It is used to 0 characters 4000 2 times n bytes


store fixed-length characters
Unicode character
data.

nvarchar It is used to store variable-length Unicode 0 4000 characters n bytes + 2


character data. characters 2 times bytes

ntext It is used to store variable-length Unicode 0 1,073,741,823 2 times the


string data. It is recommended to avoid this characters characters string length
data type because it would be deprecated in
future releases.

Binary data types


This data type allows storing image, audio, and video files of fixed and variable length into a database location. It stores information
in 0 and 1 format. The following table illustrates all binary string data type support in SQL Server:

Data Descriptions Lower Upper Range Storage


Type Range

binary It is used to store fixed-length binary strings. 0 bytes 8000 bytes n bytes

varbinary It is used to store variable-length binary string. 0 bytes 8000 bytes The actual length
of data entered + 2
bytes

image It is similar to the varbinary data type that can store up 0 bytes 2,147,483,647
to 2 GB. It is recommended to avoid this data type bytes
because it would be deprecated in future releases.

Special data types


SQL Server also provides some special data types that can be used according to our requirements. The below table illustrates all
special datatypes:
Data Type Descriptions

cursor It is an object data type used for variable and stored procedure OUTPUT parameters containing a
reference to a cursor. In addition, it stores temporary table information.. Its output is a column of
sp_cursor_list and sp_describe_cursor that returns the name of the cursor variable.

rowversion It's a data type that returns automatically generated unique binary numbers within a database. It
is commonly used for version-stamping table rows. Its memory storage is 8 bytes. It's an increasing
number that doesn't preserver time and date.

hierarchyid It is a system data type with variable length representing a position in a hierarchy. Its value
represents the position in a tree hierarchy.

uniqueidentifier This data type indicates the 16 byte GUID. It is used to convert a string constant in the form
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. Here, x is a hexadecimal digit (0-9 and a-f).

XML This data type is used to store XML data in a SQL Server table. Its storage limit is 2 GB.

Spatial Geometry It is a spatial planar data type used to represents data in a flat (Euclidean) coordinate system.
Types

Spatial Geography It is a geography spatial data type used for storing ellipsoidal (round-earth) data, such as GPS
Types latitude and longitude coordinates.

sql_variant This data type is used to store values of other SQL Server data types.
table It is a spatial data type used to store the temporary result in a table-valued function. This data is
used for later processing. We can use it by using the syntax as DECLARE @local_variable. It can
also be used in functions, stored procedures, and batches.

Conclusion
This article gives a brief overview of all data types supported in SQL Server. After knowing these data types, we will be aware of using
the appropriate data type for better storage, query processing, and data retrieval.

You might also like