While dealing with table structure in SQL Server we have to understand the basic data types in SQL Server and there difference.
To use the data type first need to understand there exact use as per software application requirement. Below explain data types in SQL Server with examples.
All below are basic data types in SQL server 2008 onward. Each data types in SQL Server has there own role and use, we can
create our own user define data types in SQL Server.
Table of Contents
Below image show the bifurcation of basic data types.
All numerical data types in SQL Server is used to store the non-decimal value. The main difference between the numeric data type is range of values, and the size taken by value to store in memory. I use the variable to explain the data type difference.
Example
Output
Check below example, if we overflow the range of TINY INT. This overflow case is applicable in all data types, if data type range exceed then it will give an error.
Example
Output
Example
Output
Example
Output
Example
Output
Below table show the What is difference between TINYINT, SMALLINT, INT and BIGINT numerical data type?
DATA TYPE | TINYINT | SMALLINT | INT | BIGINT |
---|---|---|---|---|
Size | 1 Byte | 2 Byte | 4 Byte | 8 Byte |
Range | 0 to 255 |
-32,768 to 32,768 |
-2,147,483,648 to 2,147,483,648 |
-9,223,372,036,854,775,808
to 9,223,372,036,854,775,808 |
DECIMAL data type we used to store the decimal / floating data which having precision. In SQL Server NUMERIC also same work as DECIMAL. We always need to assign decimal value in P, S, like Decimal (P, S) or Numeric (P, S) P is precision and S is scale. Storage size of decimal is depend on precision, same you can see in below table.
Precision | Size |
---|---|
1 - 9 | 5 Byte |
10 - 19 | 9 Byte |
20 - 28 | 13 Byte |
29-38 | 17 Byte |
When we set
DECLARE @dec DECIMAL(5,3) = 12.220
It means in decimal variable, we can store total 5 digit value. In 5 digit 3 is scale and remaining (5-3) = 2 digits are the precision. Find the below example.It means in decimal variable, we can store total 5 digit value. In 5 digit 3 is scale and remaining (5-3) = 2 digits are the precision.
Find the below example.
Example
Output
In above image it work fine because P(precision) is 2 digits, Now try to increase the P(precision) by one digit that is 3 and gives overflow error.
Example
Output
What is a difference between DECIMAL and FLOAT?
DECIMAL allow only 38 precision, if need to use more than 38 precision then go for FLOAT.
DECIMAL Range : -10E38 +1 to 10E38 -1
FLOAT Range : - 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308
DECIMAL also called Non-Approximate numeric data types, where float is called Approximate numeric data types because some cases decimal convert the value to it's near round number (You can see this in below example).
Example
Output
SQL Server provides large range of string data types in SQL Server. CHAR, VARCHAR, NCHAR, NVARCHAR are string equivalent
data type in SQL Server.
Below comparison will give you idea regarding what data type exactly we must choose to store string.
Let see What is difference between CHAR and VARCHAR data types in SQL Server?
Example
Output
Example
Output
If you check above both string data types the main difference is VARCHAR takes required memory and rest memory it releases, but in the case of CHAR it takes assigned memory.
Let see What is difference between NCHAR and NVARCHAR data types in sql server?
Example
Output
Example
Output
If you check above both string data types the main difference is NVARCHAR takes required memory and rest memory it releases, but in case of NCHAR it takes assigned memory.
Below table shows what is exact difference between CHAR, VARCHAR, NCHAR AND NVARCHAR?
DATA TYPE | CHAR | VARCHAR | NCHAR | NVARCHAR |
---|---|---|---|---|
Size | 1 byte for 1 char | 1 byte for 1 char | 2 byte for 1 char | 2 byte for 1 char |
Max Ragent | 8000 byte | 8000 byte | 4000 byte | 4000 byte |
Alpha-Numeric | Not Allow | Not Allow | Allow | Allow |
Memory Allocation | take assign space | take space as value assigned | take the assign space | take space as value assigned |
SQL Server provides us verity range of date time data types. To store date time in SQL Server we use the
DATE, DATETIME, DATETIME2, SMALLDATETIME and TIME and all valid data types in SQL Server 2008 on-word.
Below are examples of data types in SQL Server use to explain all date time data types with there exact difference .
Most of the time interview ask the difference between all MS SQL date time data types.
Example
Output
Example
Output
Example
Output
Example
Output
Example
Output
Below table shows What is exact difference between TIME, DATE, SMALLDATETIME, DATETIME AND DATETIME2?
DATA TYPE | TIME | DATE | SMALLDATETIME | DATETIME | DATETIME2 |
---|---|---|---|---|---|
Memory | 5 byte | 3 byte | 4 byte | 8 byte | 8 byte |
Date / Time | TIME | DATE | DATE / TIME | DATE / TIME | DATE / TIME |
Format | hh:mm:ss[.nnnnnnn] | YYYY-MM-DD | YYYY-MM-DD hh:mm:ss | YYYY-MM-DD hh:mm:ss[.nnn] | YYYY-MM-DD hh:mm:ss[.nnnnnnn] |
Range | 00:00:00.0000000 to 23:59:59.9999999 |
0001-01-01 to 9999-12-31 |
900-01-01 00:00:00 to 2079-06-06 23:59:59 |
1753-01-01 to 9999-12-31 |
0001-01-01 00:00:00.0000000 to 9999-12-31 23:59:59.9999999 |
Find list of SQL Server data types in details on Microsoft.