Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

SQL Server data types

2 October 2020

SQL Server data types

The data type is defined as the type of data that any column or variable can store in MS SQL Server.

What is the data type?

When you create any table or variable, in addition to specifying a name, you also specify the data type that it will store.

How to use MS SQL data type

  • It is necessary to define in advance the type of data that a column or a variable can store. The definition of the data type also limits the user from entering any unexpected or invalid data.
  • You can effectively use memory by assigning the appropriate data type to a variable or column that will allocate only the necessary amount of system memory for the data in the corresponding column.
  • MS SQL offers a wide category of data types according to user needs. Like a date, binary images, etc.

Why use DataTypes?

Let’s take a sample of a simple web site application registration page. Three input fields: First name, Last name, and Contact number.

Here it should be noted that in real-time:

  • “The Name/Surname will always be alphabetical.
  • The “Contact” will always be numeric.

Why use DataTypes

  • From the above figure, you should specify “Name/Surname” as a symbol and “Contact” as an integer.
    Obviously, in any application, all fields have one or the other data type. For example, numerical, alphabetic, date, and much more.

Also, note that different types of data have different memory requirements. So it makes sense to define a column or a variable with the type of data it will store for efficient memory usage.

The data type is available in MS SQL

MS SQL Server supports the following categories of data type:

  • Exact number
  • Approximate numerical
  • Date and time
  • Character strings
  • Unicode Character Strings
  • Binary rows
  • Other types of data

Data type is available in MS SQL

Exact number

An exact number has nine types of data types.

Accurate numeric data types

Data typeDescriptionLower limitUpper limitMemory
BIGINTIt stores integers in the specified range-2 ^ 63 (-9 223 372, 036 854 775 808)2 ^ 63−1 (−9 223 372, 036 854 775 807)8 bytes
INTIt stores integers in the specified range−2 ^ 31 (-2,147, 483,648)2 ^ 31−1 (-2,147, 483,647)4 bytes
SMALLINTIt stores integers in the specified range−2 ^ 15 (−32,767)2 ^ 15 (−32 768)2 bytes
TINYINTIt stores integers in the specified range02551 byte
a littleCan take values 0, 1 or NULL011 byte / 8-bit column
decimalUsed for numbers and fixed precision numbers-10 ^ 38 + 110 ^ 381-15 to 17 bytes
numericUsed for numbers 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
small moneyUsed monetary data-214,478.3648+214,478.36474 bytes

Examples:

Request:

DECLARE @Datatype_Int INT = 2
PRINT @Datatype_Int

Exit: 2

Syntax: decimal (P, S)

Here you go,

  • Accuracy
  • S is the scale

Request:

DECLARE @Datatype_Decimal DECIMAL (3.2) = 2.31
PRINT @Datatype_Decimal

Output: 2.31

Approximate Numerical

The approximate numerical category includes floating-point numbers and actual values. They are mainly used in scientific calculations.

Approximate numeric data type

Data typeDescriptionLower limitUpper limitMemoryAccuracy
Float (n)Used for a floating precision number-1.79E + 3081.79E + 308Depends on the value of n7 digits
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 a mantissa with a floating-point in a scientific record. By default, n is 53.

When a user defines a data type such as float, n should be between 1 and 53.

SQL Server handles 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 of a query:

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

Exit: 22.1234

Date and time

This is where data like date and time are stored.

Date and Time Data type

Data typeDescriptionStorage SizeaccuracyLower rangeUpper Range
DateTimeUsed to specify the date and time from January 1, 1753 to December 31, 9999. Accuracy is 3.33 milliseconds.8 bytesRounded in .000, .003, .007 steps1753-01-019999-12-31
smalldatetimeUsed to specify the date and time from 1 January 0001 to 31 December 9999. It has an accuracy of 100 nanoseconds4 bytes, corrected1 minute1900-01-012079-06-06
dateUsed for storage only dates from 1 January 0001 to 31 December 99993 bytes, corrected1 day0001-01-019999-12-31
timeIt is used to store only time values with an accuracy of 100 nanoseconds5 bytes100 nanoseconds00: 00: 00,000000023: 59: 59,9999999
DateTimeOffsetSimilar to the time data, but has a time zone offset10 bytes100 nanoseconds0001-01-019999-12-31
datetime2Used to specify the date and time from 1 January 0001 to 31 December 99996 bytes100 nanoseconds0001-01-019999-12-31

Example of request:

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

Output: “2030-01-01”

Character strings

This category refers to the type of characters. It allows the user to define a character data type that can be fixed and variable in length. It has four types of data types.

Data type Character strings

Data typeDescriptionLower limitUpper limitMemory
CHARThis is a string of characters with a fixed width. It stores no more than 8000 characters.0 characters8000 charactersn bytes
VARCHARThis is a string of characters with a variable width0 characters8000 charactersn bytes+ 2 byte
VARCHAR (Max)This is a string of characters with variable width. It stores no more than 1 073 741 824 characters.0 characters2 ^ 31 charactersn bytes + 2 byte
TextThis is a string of characters with variable width. It stores a maximum of 2 GB of text data.0 characters2 147 483 647 charactersn bytes + 4 byte

Example of request:

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

Conclusion: this is the character’s data type

Unicode Character Strings

This category stores the entire range of Unicode characters, which uses UTF-16 encoding.

Unicode character string data types

Data typeDescriptionLower limitUpper limitMemory
NCHARThis is a fixed-width Unicode string0 characters4000 characters2 times n bytes
NVARCHARThis is a Unicode string of variable width0 characters4000 characters2 times n bytes+ 2 byte
NTEXTThis is a Unicode string of variable width0 characters1 073 741 823 symbol2 times the length of the line

Example of request:

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

Conclusion: this is nCharacter Datatype

Binary string

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

Binary String Data Types

Data typeDescriptionLower limitUpper limitMemory
BinaryThis is a binary string of fixed width. It stores a maximum of 8,000 bytes.0 byte8000 byten bytes
VARBINARYThis is a binary string of variable width. It stores a maximum of 8000 bytes.0 byte8000 byteThe actual length of entered data + 2 bytes
ImageThis is a binary string of variable width. It stores a maximum of 2 GB.0 byte2 147 483 647 byte

Example of request:

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

Output: 0x000C

Other types of data

These are the other data types described below:

Data typeDescription
CursorIts output is the column sp_cursor_list and sp_describe_cursor. Returns the name of the cursor variable
String versionThis version marks the rows in the table
hierarchyid`onThis data type represents the position in the hierarchy
Unique identifierConversion from a symbolic expression.
sQL_VARIANTIt stores the values of data types supported by the SQL server
XMLIt stores XML data in a column
Type of spatial geometryIt represents data in a flat coordinate system
Type of spatial geographyIt presents data in a circular coordinate system
TableIt stores a set of results for further processing

Interesting facts!

CHAR data type is faster than VARCHAR when receiving data.

Summary:

  • Each column in a table is defined by its data type when creating the table.
  • There are six main categories and one more category. The other different ones have nine subcategories of available data types.

 

 
Tags: , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...

Preamble​​When administering PostgreSQL database servers, one of the most common tasks you will probably perform is enumerating databases and their tables....