SQL
Topperworld.in
Data Types
• Data types are used to represent the nature of the data that can be stored in
the database table. For example, in a particular column of a table, if we want
to store a string type of data then we will have to declare a string data type
of this column.
• An SQL developer must be aware of what type of data will be stored inside
each column while creating a table.
• The data type guideline for SQL is to understand what type of data is
expected inside each column and it also identifies how SQL will interact with
the stored data.
For every database, data types are primarily classified into three categories.
1) Numeric Datatypes
2) Date and Time Database
3) String Database
❖ Numeric Data Types
• Exact Numeric Datatype
There are nine subtypes which are given below in the table. The table
contains the range of data in a particular type.
©Topperworld
SQL
Data Type From To
-263 (- 263 -1
BigInt
9,223,372,036,854,775,808) (9,223,372,036,854,775,807)
Int -231 (-2,147,483,648) 231-1 (2,147,483,647)
smallint -215 (-32,768) 215-1 (32,767)
tinyint 0 28-1 (255)
bit 0 1
decimal -1038+1 1038-1
numeric -1038+1 1038-1
money -922,337,203,685,477.5808 922,337,203,685,477.5807
smallmoney -214,748.3648 214,748.3647
©Topperworld
SQL
• Approximate Numeric Datatype
The subtypes of this datatype are given in the table with the range.
Data Type From To
Float -1.79E+308 1.79E+308
Real -3.40E+38 3.40E+38
❖ String Data Types
• Character String Datatype
The subtypes are given in below table –
Data Type Description
The maximum length of 8000 characters.(Fixed-Length non-
char
Unicode Characters)
The maximum length of 8000 characters.(Variable-Length
varchar
non-Unicode Characters)
The maximum length of 231 characters(SQL Server 2005
varchar(max)
only).(Variable Length non-Unicode data)
©Topperworld
SQL
Data Type Description
The maximum length of 2,127,483,647 characters(Variable
text
Length non-Unicode data)
Unicode Character String Datatype
The details are given in below table –
Data Type Description
The maximum length of 4000 characters(Fixed-Length
nchar
Unicode Characters)
The maximum length of 4000 characters.(Variable-Length
Nvarchar
Unicode Characters)
The maximum length of 231 characters(SQL Server 2005
nvarchar(max)
only).(Variable Length Unicode data)
©Topperworld
SQL
Server String Data Type in SQL
There are four subtypes of this datatype which are given below:
Datatypes Description
The maximum length of 8000 bytes(Fixed-Length binary
Binary
data)
The maximum length of 8000 bytes(Variable Length
varbinary
binary data)
The maximum length of 231 bytes(SQL Server 2005
varbinary(max)
only).(Variable Length binary data)
Maximum Length of 2,147,483,647 bytes(Variable
text
Length binary data)
❖ Server Date and Time Data Type
The details are given in the below table.
Data Type Description
DATE A data type is used to store the data of date in a record
©Topperworld
SQL
Data Type Description
TIME A data type is used to store the data of time in a record
A data type is used to store both the data,date, and time in the
DATETIME
record.
❖ Other Data Types
XML Datatype
XML data type allows storage of XML documents and fragments in a
SQL Server database
DataType Description
XML A Datatype used to store data in the format of XML
Datatype datatype
Spatial Dataype
A datatype is used for storing planar spatial data, such as points, lines, and
polygons, in a database table.
©Topperworld
SQL
DataType Description
A datatype is used for storing planar spatial data, such as
Geometry
points, lines, and polygons, in a database table.
Array Datatype
SQL Server does not have a built-in array datatype. However, it is possible
to simulate arrays using tables or XML data types.
❖ Properties of data types
SQL data types define the type of data that can be stored in a database column
or variable. Here are the most common SQL data types:
Datatype Properties
Numeric data These are used to store numeric values. Examples
types include INT, BIGINT, DECIMAL, and FLOAT.
Character data These are used to store character strings. Examples
types include CHAR, VARCHAR, and TEXT.
Date and time These are used to store date and time values. Examples
data types include DATE, TIME, and TIMESTAMP
©Topperworld
SQL
Datatype Properties
Binary data These are used to store binary data, such as images or
types audio files. Examples include BLOB and BYTEA.
Boolean data This data type is used to store logical values. The only
type possible values are TRUE and FALSE.
These are used to store intervals of time. Examples
Interval data
include INTERVAL YEAR, INTERVAL MONTH, and
types
INTERVAL DAY.
Array data These are used to store arrays of values. Examples
types include ARRAY and JSON.
XML data type This data type is used to store XML data.
Spatial data These are used to store geometric or geographic data.
types Examples include POINT, LINE, and POLYGON.
Different databases may have different variations of these data types, or
they may have additional data types not listed here. Understanding SQL
data types are important for creating tables and working with data in a
database, as it affects how data is stored and processed.
©Topperworld