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.

  • ๐Ÿ”ข Exact Numeric: bigint, int, smallint, tinyint, bit, decimal, numeric, money, and smallmoney store precise whole and fixed-point numbers.
  • ๏ธ Approximate Numeric: float and real store approximate floating-point values used in scientific calculations.
  • ???? Data e ora: datetime, smalldatetime, date, time, datetimeoffset, and datetime2 store temporal values.
  • ๐Ÿ”ค Character and Unicode: char, varchar, and text hold text; nchar, nvarchar, and ntext hold Unicode text.
  • ๏ธ Binary and Other: binary, varbinary, and image store byte data; cursor, xml, and uniqueidentifier cover special needs.
  • ๐Ÿง  La scelta di WiseLY: matching each column to the smallest suitable type improves storage efficiency and data integrity.

Tipi di dati di SQL Server con esempi

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.

Sign-up form example defining First and Last Name as characters and Contact as an integer

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.

Diagram grouping MS SQL Server data types into their seven 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.

DOMANDE FREQUENTI

VARCHAR stores non-Unicode characters using one byte each, while NVARCHAR stores Unicode characters using two bytes each. Choose NVARCHAR when a column must hold multiple languages or symbols outside a single code page; otherwise VARCHAR uses less storage.

Use DECIMAL (or NUMERIC) when values must be exact, such as money or measurements, because it stores fixed precision. Use FLOAT for approximate scientific values where tiny rounding differences are acceptable and a very wide range matters more than exactness.

Yes. In SQL Server, DECIMAL and NUMERIC are functionally identical. Both accept a precision and scale and store exact fixed-point numbers, so the two keywords are interchangeable. You can use whichever name your team prefers for readability.

CHAR is a fixed-length type that pads shorter values with spaces, so every value uses the declared width. VARCHAR is variable-length and stores only the characters entered plus two bytes. Use CHAR for uniform codes and VARCHAR for text of varying length.

Sรฌ. Le serrature scorrevoli portatili e i catenacci a superficie possono essere usati per mettere in sicurezza una porta a scomparsa dall'esterno. Alcuni kit con catena di sicurezza consentono anche il bloccaggio esterno con chiave o manopola girevole. ALTER TABLE table_name ALTER COLUMN column_name new_type changes a column data type. The change succeeds only when existing values convert cleanly to the new type; otherwise it fails, so test conversions and back up data first.

Regular VARCHAR(n) stores up to 8,000 characters. VARCHAR(max) stores up to about 2 GB (2^31-1 bytes) and is meant for large text that exceeds 8,000 characters. Use VARCHAR(max) only when values can genuinely be that large.

Sรฌ. Le serrature scorrevoli portatili e i catenacci a superficie possono essere usati per mettere in sicurezza una porta a scomparsa dall'esterno. Alcuni kit con catena di sicurezza consentono anche il bloccaggio esterno con chiave o manopola girevole. Copilota GitHub can suggest column data types and generate DECLARE and CREATE TABLE statements from natural-language prompts. Always confirm precision, scale, length, and Unicode needs against your data before applying its suggestions in production.

AI and machine learning tools review sample data to recommend suitable types, precision, and lengths, flag oversized columns that waste storage, and detect mismatches during migrations. They speed up schema design while the developer confirms the final choices.

Riassumi questo post con: