Tipi di dati di SQL Server con esempi
โก Riepilogo intelligente
SQL Server data types define the kind of value a column or variable can store, such as integer, decimal, character, date, or binary. Choosing the correct type enforces valid data and uses memory efficiently.

Cos'รจ il tipo di dati?
A data type in MS SQL Server is defined as the type of data that any column or variable can store. It is the kind of data that an object holds, such as integer, character, or string. While creating any tavolo or variabile, in addition to specifying the name, you also set the type of data it will store.
Come utilizzare il tipo di dati MS SQL
You need to define, in advance, the type of data a column or variable can store. Determining the 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 a variable or column, which allocates only the required amount of system memory for that columnโs data.
MS SQL offers a broad range of basic data types in SQL to suit user needs, such as date, binary, images, and more.
Perchรฉ utilizzare i tipi di dati?
Consider the example of a simple sign-up page for a website application. The three input fields are First Name, Last Name, and Contact number.
Here we should note that, in real scenarios:
- โFirst/Last Nameโ will always be alphabetic.
- โContactโ will always be numeric.
The example below illustrates these fields on a sign-up form.
From the picture above, it makes sense to define โFirst/Last Nameโ as a character and โContactโ as an integer.
It is evident that in any application, all fields have one or another type of data, for example numeric, alphabetic, date, and many more.
Also note that different data types have different memory requirements. Therefore, it makes more sense to define the column or variable with the data type it will hold, for efficient use of memory.
Tipo di dati disponibile in MS SQL Server
Here is the MS SQL Server data types list. MS SQL Server supports the following categories of data type:
- Numerico esatto
- Numerico approssimativo
- Data e ora
- Stringhe di caratteri
- Stringhe di caratteri Unicode
- Stringhe binarie
- Altri tipi di dati
The diagram below groups the MS SQL Server data types into these categories.
Tipi di dati numerici esatti in SQL
Exact numeric has nine sub data types in SQL Server. Each stores whole or fixed-point numbers within a defined range.
| Tipo di dati | Descrizione | Limite inferiore | Limite superiore | Memorie |
|---|---|---|---|---|
| grande | Memorizza numeri interi nell'intervallo indicato | โ2^63 (โ9,223,372,036,854,775,808) | 2^63โ1 (9,223,372,036,854,775,807) | 8 byte |
| int | Memorizza numeri interi nell'intervallo indicato | โ2^31 (โ2,147,483,648) | 2^31โ1 (2,147,483,647) | 4 byte |
| piccolo | Memorizza numeri interi nell'intervallo indicato | โ2^15 (โ32,768) | 2^15โ1 (32,767) | 2 byte |
| minuscolo | Memorizza numeri interi nell'intervallo indicato | 0 | 255 | 1 byte |
| bit | Puรฒ assumere valori 0, 1 o NULL. | 0 | 1 | 1 byte / 8-bit column |
| decimale | Utilizzato per numeri in scala e precisione fissa | โ10^38+1 | 10^38โ1 | Da 5 a 17 byte |
| numerico | Utilizzato per numeri in scala e precisione fissa | โ10^38+1 | 10^38โ1 | Da 5 a 17 byte |
| soldi | Used for monetary data | -922,337,203,685,477.5808 | +922,337,203,685,477.5807 | 8 byte |
| smallmoney | Used for monetary data | -214,478.3648 | +214,478.3647 | 4 byte |
Exact Numeric data types in SQL Server with examples:
Query:
DECLARE @Datatype_Int INT = 2 PRINT @Datatype_Int
Produzione:
2
Sintassi:
Decimal (P,S)
Here, P is the precision and S is the scale.
Query:
DECLARE @Datatype_Decimal DECIMAL (3,2) = 2.31 PRINT @Datatype_Decimal
Produzione:
2.31
Tipi di dati numerici approssimativi in โโSQL
The SQL approximate numeric category includes floating-point and real values. These data types are mostly used in scientific calculations.
| Tipo di dati | Descrizione | Limite inferiore | Limite superiore | Memorie | Precisione |
|---|---|---|---|---|---|
| galleggiante(n) | Utilizzato per un numero con precisione mobile | โ1.79E+308 | 1.79 308 + | Dipende dal valore di n | 15 cifre |
| di rose | Utilizzato per un numero con precisione mobile | โ3.40E+38 | 3.40 38 + | 4 byte | 7 cifre |
The float type uses the following syntax:
Syntax: FLOAT [(n)]
Here, n is the number of bits used to store the mantissa of the float number in scientific notation. By default, the value of n is 53.
When you define 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.
Interrogazione di esempio:
DECLARE @Datatype_Float FLOAT(24) = 22.1234 PRINT @Datatype_Float
Produzione:
22.1234
Tipi di dati di data e ora in SQL
This category stores data of the date and time type.
| Tipo di dati | Descrizione | Dimensioni di archiviazione | Precisione | Gamma inferiore | Gamma superiore |
|---|---|---|---|---|---|
| Appuntamento | Utilizzato per specificare una data e un'ora dal 1 gennaio 1753 al 31 dicembre 9999. Ha una precisione di 3.33 millisecondi. | 8 byte | Arrotondato a incrementi di .000, .003, .007 | 1753-01-01 | 9999-12-31 |
| smalldatetime | Used for specifying a date and time from January 1, 1900 to June 6, 2079. It has an accuracy of 1 minute. | 4 byte, fissi | minuti 1 | 1900-01-01 | 2079-06-06 |
| quando | Used to store only a date from January 1, 0001 to December 31, 9999 | 3 byte, fissi | 1 giorno | 0001-01-01 | 9999-12-31 |
| tempo | Used for storing only time values with an accuracy of 100 nanoseconds. | 5 byte | 100 nanosecondi | 00:00:00.0000000 | 23:59:59.9999999 |
| datatimeoffset | Similar to datetime but has a time zone offset | 10 byte | 100 nanosecondi | 0001-01-01 | 9999-12-31 |
| data e ora2 | Utilizzato per specificare una data e un'ora dal 1 gennaio 0001 al 31 dicembre 9999 | 6 byte | 100 nanosecondi | 0001-01-01 | 9999-12-31 |
Interrogazione di esempio:
DECLARE @Datatype_Date DATE = '2030-01-01' PRINT @Datatype_Date
Produzione:
'2030-01-01'
Tipi di dati delle stringhe di caratteri in SQL
This category relates to character types. It lets you define character data of fixed and variable length. It has four kinds of data types. Below are the character string data types with examples.
| Tipo di dati | Descrizione | Limite inferiore | Limite superiore | Memorie |
|---|---|---|---|---|
| serbatoio | ร una stringa di caratteri con larghezza fissa. Memorizza un massimo di 8,000 caratteri. | 0 caratteri | 8000 caratteri | n byte |
| varchar | This is a character string with a variable width. | 0 caratteri | 8000 caratteri | n byte + 2 byte |
| varchar (massimo) | Questa รจ una stringa di caratteri con larghezza variabile. Memorizza un massimo di 1,073,741,824 caratteri. | 0 caratteri | 2^31 caratteri | n byte + 2 byte |
| testo | This is a character string with a variable width. It stores a maximum of 2 GB of text data. | 0 caratteri | 2,147,483,647 caratteri | n byte + 4 byte |
Interrogazione di esempio:
DECLARE @Datatype_Char VARCHAR(30) = 'This is Character Datatype' PRINT @Datatype_Char
Produzione:
Questo รจ il tipo di dati carattere
Tipi di dati delle stringhe di caratteri Unicode in SQL
This category stores the full range of Unicode characters, which use the UTF-16 character encoding.
| Tipo di dati | Descrizione | Limite inferiore | Limite superiore | Memorie |
|---|---|---|---|---|
| nchar | It is a Unicode string of fixed width. | 0 caratteri | 4000 caratteri | 2 volte n byte |
| nvarchar | It is a Unicode string of variable width. | 0 caratteri | 4000 caratteri | 2 volte n byte + 2 byte |
| ntext | It is a Unicode string of variable width. | 0 caratteri | 1,073,741,823 caratteri | 2 volte la lunghezza della corda |
Interrogazione di esempio:
DECLARE @Datatype_nChar VARCHAR(30) = 'This is nCharacter Datatype' PRINT @Datatype_nChar
Produzione:
Questo รจ il tipo di dati nCharacter
Tipi di dati di stringa binaria in SQL
This category contains binary strings of fixed and variable length.
| Tipo di dati | Descrizione | Limite inferiore | Limite superiore | Memorie |
|---|---|---|---|---|
| binario | It is a fixed-width binary string. It stores a maximum of 8,000 bytes. | 0 byte | 8000 byte | n byte |
| varbinary | This is a binary string of variable width. It stores a maximum of 8,000 bytes. | 0 byte | 8000 byte | La lunghezza effettiva dei dati immessi + 2 byte |
| Immagine | This is a binary string of variable width. It stores a maximum of 2 GB. | 0 byte | 2,147,483,647 byte | - |
Interrogazione di esempio:
DECLARE @Datatype_Binary BINARY(2) = 12; PRINT @Datatype_Binary
Produzione:
0x000C
Altri tipi di dati in SQL
These are the other SQL Server data types, with their descriptions below:
| Tipo di dati | Descrizione |
|---|---|
| Cursore | Its output is a column of sp_cursor_list and sp_describe_cursor. It returns the name of the cursor variable. |
| Rowversion | It version-stamps table rows. |
| Gerarchia | This data type represents a position in a hierarchy. |
| Identificativo unico | Stores a GUID, often converted from a character expression. |
| SQL_variante | It stores values of SQL Server-supported data types. |
| XML | Memorizza i dati XML in una colonna. |
| Tipo di geometria spaziale | Rappresenta i dati in un sistema di coordinate piatte. |
| Tipo di geografia spaziale | Rappresenta i dati nel sistema di coordinate della Terra Rotonda. |
| tavolo | Memorizza un set di risultati per un'elaborazione successiva. |
Fatti interessanti!
- The CHAR data type is faster than the VARCHAR data type while retrieving data, because its fixed width avoids length bookkeeping.


