-->
SQL Data Types: Different sql server data types with EXAMPLES

SQL Data Types: Different sql server data types with EXAMPLES

 


What is Data Type?

Data Type in SQL server is defined as the type of data that any column or variable can store. It is a type of data that an object holds like integer, character, string, etc. While creating any table or variable, in addition to specifying the name, you also set the Type of Data it will store.

How to use MS SQL datatype

  • You need to define in advance, the type of data a column or variable can store. Determining data type also restricts the user from entering any unexpected or invalid data.
  • You can make efficient use of memory by assigning an appropriate data type to variable or column which will allocate only the required amount of system memory for the respective column’s data.
  • MS SQL offers a broad category of basic data types in SQL as per user’s needs like Date, binary images, etc.

In this tutorial, you will learn MS SQL data types with examples:

Why use DataTypes?

Let’s, take a sample of simple Sign up page of website application.Three input fields are First Name, Last Name & Contact number.

Here we should note that in real time:

  • “First/Last Name” will always be alphabetic.
  • “Contact” will always be numeric.
  • From the above picture it worth defining “First/Last Name” as a character and “Contact” as an integer.

It is evident that in any application, all fields have one or the other type of data. E.g., numeric, alphabetic, date, and many more.

Also, note that different datatype has different memory requirement. Therefore, it makes more sense to define the column or variable with the data type it will hold for efficient use of memory.

Data type available in MS SQL Server

Here is MS SQL server data types list:

MS SQL server support following categories of Data type:

  • Exact numeric
  • Approximate numeric
  • Date and time
  • Character strings
  • Unicode character strings
  • Binary strings
  • Other data types


Exact Numeric Data Types in SQL

Exact numeric has nine types of sub data types in SQL server.

Exact Numeric Data Types

Data TypeDescriptionLower limitUpper limitMemory
bigintIt stores whole numbers in the range given−2^63 (−9,223,372, 036,854,775,808)2^63−1 (−9,223,372, 036,854,775,807)8 bytes
intIt stores whole numbers in the range given−2^31 (−2,147, 483,648)2^31−1 (−2,147, 483,647)4 bytes
smallintIt stores whole numbers in the range given−2^15 (−32,767)2^15 (−32,768)2 bytes
tinyintIt stores whole numbers in the range given02551 byte
bitIt can take 0, 1, or NULL values.011 byte/8bit column
decimalUsed for scale and fixed precision numbers−10^38+110^381−15 to 17 bytes
numericUsed for scale and fixed precision numbers−10^38+110^381−15 to 17 bytes
moneyUsed monetary data−922,337, 203, 685,477.5808+922,337, 203, 685,477.58078 bytes
smallmoneyUsed monetary data−214,478.3648+214,478.36474 bytes

Exact Numeric data types in SQL server with Examples:

Query:

DECLARE @Datatype_Int INT = 2
PRINT @Datatype_Int

Output:

2

Syntax:

Decimal (P,S)

Here,

  • P is precision
  • S is scale

Query:

DECLARE @Datatype_Decimal DECIMAL (3,2) = 2.31
PRINT @Datatype_Decimal

Output:

2.31

Approximate Numeric Data Types in SQL

SQL Approximate Numeric category includes floating point and real values. These datatypes in SQL are mostly used in scientific calculations.

Approximate Numeric Data Type

Data TypeDescriptionLower limitUpper limitMemoryPrecision
float(n)Used for a floating precision number−1.79E+3081.79E+308Depends on the value of n7 Digit
realUsed for a floating precision number−3.40E+383.40E+384 bytes15 Digit
Syntax:  FLOAT [(n)]

Here, n is the number of bits that are used to store the mantissa of the float number in scientific notation. By default, the value of n is 53.

When the user defines a data type like float, n should be a value between 1 and 53.

SQL Server treats n as one of two possible values. If 1<=n<=24, n is treated as 24. If 25<=n<=53, n is treated as 53.

Example Query:

DECLARE @Datatype_Float FLOAT(24) = 22.1234
PRINT @Datatype_Float

Output:

22.1234

Date and Time Data Types in SQL

It stores data of type Date and time.

Date and Time Data Type

Data TypeDescriptionStorage sizeAccuracyLower RangeUpper Range
DateTimeUsed for specifying a date and time from January 1, 1753 to December 31, 9999. It has an accuracy of 3.33 milliseconds.8 bytesRounded to increments of .000, .003, .0071753-01-019999-12-31
smalldatetimeUsed for specifying a date and time from January 1, 0001 to December 31, 9999. It has an accuracy of 100 nanoseconds4 bytes, fixed1 minute1900-01-012079-06-06
dateUsed to store only date from January 1, 0001 to December 31, 99993 bytes, fixed1 day0001-01-019999-12-31
timeUsed for storing only time only values with an accuracy of 100 nanoseconds.5 bytes100 nanoseconds00:00:00.000000023:59:59.9999999
datetimeoffsetSimilar to datatime but has a time zone offset10 bytes100 nanoseconds0001-01-019999-12-31
datetime2Used for specifying a date and time from January 1, 0001 to December 31, 99996 bytes100 nanoseconds0001-01-019999-12-31

Example Query :

DECLARE @Datatype_Date DATE = '2030-01-01'
PRINT @Datatype_Date

Output:

‘2030-01-01’

Character Strings Data Types in SQL

This category is related to a character type. It allows the user to define the data type of character which can be of fixed and variable length. It has four kinds of data types. Below are the character string SQL server data types with examples.

Character Strings Data Types

Data TypeDescriptionLower limitUpper limitMemory
charIt is a character string with a fixed width. It stores a maximum of 8,000 characters.0 chars8000 charsn bytes
varcharThis is a character string with variable width0 chars8000 charsn bytes + 2 bytes
varchar (max)This is a character string with a variable width. It stores a maximum of 1,073,741,824 characters.0 chars2^31 charsn bytes + 2 bytes
textThis is a character string with a variable width. It stores a maximum 2GB of text data.0 chars2,147,483,647 charsn bytes + 4 bytes

Example Query :

DECLARE @Datatype_Char VARCHAR(30) = 'This is Character Datatype'
PRINT @Datatype_Char

Output:

This is Character Datatype

Unicode Character Strings Data Types in SQL

This category store the full range of Unicode character which uses the UTF-16 character encoding.

Unicode Character String Data Types

Data TypeDescriptionLower limitUpper limitMemory
ncharIt is a Unicode string of fixed width0 chars4000 chars2 times n bytes
nvarcharIt is a unicode string of variable width0 chars4000 chars2 times n bytes + 2 bytes
ntextIt is a unicode string of variable width0 chars1,073,741,823 char2 times the string length

Example Query:

DECLARE @Datatype_nChar VARCHAR(30) = 'This is nCharacter Datatype'
PRINT @Datatype_nChar

Output:

This is nCharacter Datatype

Binary String Data Types in SQL

This category contains a binary string of fixed and variable length.

Binary String Data Types

Data TypeDescriptionLower limitUpper limitMemory
binaryIt is a fixed width binary string. It stores a maximum of 8,000 bytes.0 bytes8000 bytesn bytes
varbinaryThis is a binary string of variable width. It stores a maximum of 8,000 bytes0 bytes8000 bytesThe actual length of data entered + 2 bytes
imageThis is a binary string of variable width. It stores a maximum of 2GB.0 bytes2,147,483,647 bytes

Example Query:

DECLARE @Datatype_Binary BINARY(2) = 12;
PRINT @Datatype_Binary

Output:

0x000C

Other Datatypes in SQL

These are other different SQL server datatypes with the description below-

Data TypeDescription
CursorIts output is a column of sp_cursor_list and sp_describe_cursor.
It returns the name of the cursor variable.
Row versionIt version stamps table rows.
HierarchyidThis datatype represents a position in the hierarchy
UniqueidentifierConversion from a character expression.
Sql_variantIt stores values of SQL server supported Datatypes.
XMLIt stores XML data in a column.
Spatial Geometry typeIt represents data in a flat coordinate system.
Spatial Geography typeIt represents data in the round-earth coordinate system.
tableIt stores a result set for later processing.

Interesting Facts!

  • CHAR data type is faster than SQL VARCHAR data type while retrieving data.

Summary:

  • Every column in tables defines with its datatype during table creation.
  • There are six main categories and one other miscellaneous category. Other miscellaneous have nine subcategories of SQL server data types and sizes available.

0 Response to "SQL Data Types: Different sql server data types with EXAMPLES"

Post a Comment

Ads on article

Advertise in articles 1

advertising articles 2

Advertise under the article