Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

SQL Data Types

by suresh

The SQL data type is an attribute that specifies the type of data an object can hold. For instance, character data (string data), binary data, integer data, financial data, date and time data, and so on.

The following data types are special SQL data types to store geographical data.

  • Geometry Data type: This data type is used to represent the data in a two-dimensional Euclidean coordinate system
  • Geography Datatype: The functionality of this Sql Server datatype is the same as the Geometry. The only difference between both these data types is, when you declare the variable with Geography data type, you are specifying the points in terms of latitude and longitude.

List of available SQL Data Types

The following are the available list of SQL server Data Types and their description.

SQL Numeric Data Type

The following are the available list of numerical data types in SQL Server

SQL Data TypesDescriptionLength
intStores the integer values ranging from -2,147,483,648 to 2,147,483,6474 Bytes
tinyintStores the integer values ranging from 0 to 2551 Byte
smallintAccept integer values ranging from -32,768 to 32,7672 Byte
bigintIt stores the integer values ranging from -253 to -253 – 18 Bytes
moneyIt stores the financial values ranging from -922,337,203,685,477.5808 to -922,337,203,685,477.58078 Bytes
smallmoneyIt stores the financial values ranging from -214,748.3648 to 214,748.36474 Bytes
decimal(p,s)It stores the decimal values. Here p is the Precision, s is the Scale, and the maximum precision value is 38 digits.5 – 17 Bytes
numeric(p,s)Functionality of this data type is equivalent to decimal (p, s).5 – 17 Bytes
float(n)It stores the floating point values with precision of 7 digit (if n = 24, and 15 digit (if n = 53).5 – 17 Bytes5 – 17 Bytes If n = 24 it uses 4 Bytes, and If n = 53 it uses 8 Bytes
realFunctionality of this data type is equivalent to float(24).4 Bytes

SQL Character and String Data Types

These SQL data types stores n Unicode characters 2n Bytes, where the range of n is from 1 to 4000.

SQL DataTypesDescription & Length
char(n)It stores n characters Bytes, where the range of n is from 1 to 8000.
nchar(n)It stores approximately n characters Actual string length + 2 Byte, where the range of n is from 1 to 8000
varchar(n)It stores up to 231-1 characters. Length = Actual string length + 2 Byte
varchar(max)It stores approximately n characters2n(Actual string length) + 2 Byte, where the range of n is from 1 to 8000
nvarchar(n)It stores up to ((231-1) /2) – 2 characters
nvarchar(max)It stores up to ((2n-1) /2) – 2 characters. 2n(Actual string length) + 2 Byte

SQL Binary Data Types

SQL DataTypesDescriptionLength
bitIt stores a single bit of data1 byte for 8-bit column
binary(n)It stores n bytes of binary datan Bytes, where the range of n is from 1 to 8000
varbinary(n)It stores approximately n bytes of binary dataActual string length + 2 Byte, where the range of n is from 1 to 8000
varbinary(max)It stores up to 231-1 bytes of binary dataActual string length + 2 Byte

SQL Date and Time Data Types

Data TypesDescriptionLengthExample
dateIt stores dates between January 01, 0001, and December 31, 99993 Bytes2016-08-19
DateTimeIt stores date and time between January 01, 1753, and December 31, 99998 Bytes2016-08-19 10:15:45.0110
datetime2It stores date and time between January 01, 0001, and December 31, 99996 – 8 Bytes2016-08-19 10:15:45.0110112
datetimeoffsetIt is same as datetime2, and in addition, it will include the Universal Time Coordinate (UTC) offset (we also called as GMT)8 – 10 Bytes2016-08-19 10:15:45.0110112 +05:30
smalldatetimeIt stores date and time between January 01, 1990, and June 06, 20794 Bytes2016-08-19
timeIt stores time with an accuracy of 100 nanoSeconds. The time range is between 00:00:00.0000000, and 23:59:59.99999993 – 5 Bytes10:15:45.0110136

Difference between Char and Varchar, Nvarchar data types in SQL Server

Char Data Type in SQL Server

Character or Char data type in Sql Server is used to store the fixed length of characters.

For example, if we declare variable, @str CHAR (50) means the SQL server will allocate memory for 50 characters. Suppose we inserted 10 characters into a @str variable, 10 characters of memory used, and the remaining 40 characters will waste.

Varchar Data Type in SQL Server

A Varchar data type in Sql Server means variable character, and it is used to store the non-Unicode characters. Varchar allocates the memory based on the number of characters inserted into it.

For example, if we declare variable, @name VARCHAR (50) means, it will allocate memory for 0 characters. Suppose we inserted 10 characters into a @name variable. Then, this SQL data type will allocate the memory to store those 10 characters (it allocate literally 10 characters and no wastage). If you think that the size of the column data might exceed 8000 bytes, please use Varchar(max).

NVarchar DataType in SQL Server

NVarchar data type in Sql Server is the same as the Varchar data type. The only difference between them is, it is used to store Unicode characters. NVarchar data type will take double (2n) space to store the other language characters. If you think that the size of the column data might exceed 4000 bytes, please use nvarchar(max).

NOTE: When you are not storing other language characters then, I suggest you choose Varchar

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy