Thứ Sáu, Tháng Ba 7, 2025
spot_img
HomeData types in SQL Server

Data types in SQL Server

Data types in SQL quite diverse. Let's find out with Quantrimang.com Data types, including numeric in SQL!

In SQL Server, each column, local variable, expression, and parameter has an associated data type. A data type is an attribute that defines the type of data an object can hold, including: integer data, characters, currency, date & time, binary strings, etc.

SQL Server provides a set of system data types that define all the data types that can be used with SQL Server. You can also specify private data types in Transact-SQL or the Microsoft.NET Framework.

In general, datatype is a data classification, telling the compiler what type of data the programmer is storing. Most programming languages ​​support different data types, such as integers, characters or strings, Boolean, etc.

What is SQL Server data type?

Datatype ensures the type of data a column or variable can store in SQL Server. When creating any table or variable, it requires specifying the data type to be stored in addition to the name of each column. For example, in SQL Server, each table has multiple columns and contains specific data types for each column. They can be integers, floats, varchar, Boolean…

How to use datatype?

You need to define the data type first for each column of a table to ensure the type of data it can store. Data type configuration also prevents users from entering any invalid or unwanted information.

When you assign the appropriate data type to a column, you can make efficient memory use by allocating only the amount of system memory needed for the data in the relevant column.

You can use the following characteristics to define data types in SQL Server:

  • The data type (fixed or variable) it represents.
  • Storage capacity depends on the value, fixed or variable length.
  • Stored values ​​may or may not be indexed.
  • How SQL Server implements a value comparison of a particular data type.

Data types in SQL Server:

  • String – String
  • Numeric – Number
  • Date and Time – Date and time

Character string data type in SQL Server

Below are the data types belonging to the character string group in SQL Server (Transact-SQL).

Data type syntax Maximum size Explain
CHAR(kich_thuoc) Maximum 8000 characters.
  • size_thuoc is the number of characters to store.
  • Fixed length.
  • Add a space to the right to compensate for the blank space for the full number of characters.
  • Does not contain Unicode characters.
VARCHAR(kich_thuoc) or VARCHAR(toi_da) Maximum 8000 characters or maximum number.
  • size_thuoc is the number of characters to store.
  • Customizable length.
  • If specified as toi_da, the maximum is 2GB.
  • Does not contain Unicode characters.
TEXT Maximum 2GB.
  • Customizable length.
  • Does not contain Unicode characters.
NCHAR(kich_thuoc) Maximum 4000 characters.
  • Fixed length.
  • Unicode characters.
NVARCHAR(kich_thuoc) or NVARCHAR(toi_da) Maximum 4000 characters or maximum number.
  • size_thuoc is the number of characters to store.
  • Customizable length.
  • If toi_da is specified, the maximum number of characters is 2GB.
  • Unicode characters.
NTEXT Maximum 1,073,741,823 bytes.
  • Customizable length.
  • Unicode characters.
BINARY(kich_thuoc) Maximum 8000 characters.
  • size_thuoc is the number of characters to store.
  • Fixed length.
  • Add spaces to compensate for the number of characters.
  • Binary data.
VARBINARY(kich_thuoc) or VARBINARY(toi_da) Maximum 8000 characters or maximum number.
  • size_thuoc is the number of characters to store.
  • Customizable length.
  • If specified as toi_da, the maximum is 2GB.
  • Binary data.
IMAGE Maximum size is 2GB.
  • Customizable length.
  • Binary data.

Data types in SQL

Numeric data type in SQL Server

Below is a list of numeric data types in SQL Server (Transact-SQL).

Data type syntax Maximum size Explain
BIT integer 0, 1 or NULL
TINYINT from 0 to 255
SMALLINT from -32768 to 32767
INT -2,147,483,648 to 2,147,483,647
BIGINT from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
DECIMAL(m,d)
  • m defaults to 18 if not specifically specified.
  • d defaults to 0 if not specifically specified.
m is the total number of numbers and d is the number of numbers after the comma.
DEC(m,d)
  • m defaults to 18 if not specifically specified.
  • d defaults to 0 if not specifically specified.

m is the total number of numbers and d is the number of numbers after the comma.

Synonymous with DECIMAL data type.

NUMERIC(m,d)
  • m defaults to 18 if not specifically specified.
  • d defaults to 0 if not specifically specified.

m is the total number of numbers and d is the number of numbers after the comma.

Synonymous with DECIMAL data type.

FLOAT(n) floating point number n defaults to 53 if not specifically specified. n is the number of bits stored in a chemical symbol.
REAL equivalent to FLOAT(24)
SMALLMONEY from – 214,748.3648 to 214,748.3647
MONEY from -922,337,203,685,477.5808 to 922,337,203,685,477.5807

Date/time data type in SQL Server

Below is a list of date/time data types in SQL Server (Transact-SQL)

Data type syntax Maximum size Explain
DATE value from '0001-01-01' to '9999-12-31. displays as 'YYYY-MM-DD'
DATETIME
  • Date taken from '1753-01-01 00:00:00' to '9999-12-31 23:59:59'.
  • Time taken from '00:00:00' to '23:59:59:997'
displays as 'YYYY-MM-DD hh:mm:ss[.mmm]
DATETIME2 (accurate to decimal seconds)
  • Values ​​range from '0001-01-01' to '9999-12-31'.
  • Time taken from '00:00:00' to '23:59:59:9999999'.
displays as 'YYYY-MM-DD hh:mm:ss[.số giây thập phân]'
SMALLDATETIME
  • value taken from '1900-01-01' to '2079-06-06'.
  • Time taken from '00:00:00' to '23:59:59'.
displays as 'YYYY-MM-DD hh:mm:ss
TIME
  • value ranges from '00:00:00.0000000' to '23:59:59.9999999'.
  • Dates taken from '0001-01-01' to '9999-12-31'.
displays as 'YYYY-MM-DD hh:mm:ss[.nnnnnnn]'
DATETIMEOFFSET (accurate to decimal seconds)
  • Time value ranges from '00:00:00' to '23:59:59:9999999'.
  • Time zone is from -14:00 to +14:00.
displays as YYYY-MM-DD hh:mm:ss[.nnnnnnn]' [{+|-}hh:mm]

Other data types in SQL Server

Data type

Describe

sql_variant

Contains up to 8,000 bytes of data of various types, excluding text, ntext, and timestamp

uniqueidentifier

Stores a globally unique identifier (GUID)

xml

Stores data in XML format. Maximum 2GB

cursor.cursor

Stores a reference to the pointer used for database operations

table. table

Store a set of results for later processing

Previous article: PIVOT clause in SQL Server

Next article: CREATE TABLE command in SQL Server

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments