Questo articolo mostra esempi di conversione di un valore smalldatetime in date in SQL Server.
La conversione riduce lo spazio occupato da 4 byte a 3 byte. L'ora viene persa, quindi questa operazione ha senso solo se non ti serve.
smalldatetime contiene data e ora, ma non ha secondi frazionari e ha accuratezza al minuto. date, invece, contiene solo la data e ha accuratezza di un giorno.
Esempio 1: conversione implicita
DECLARE @thesmalldatetime smalldatetime,
@thedate date;
SET @thesmalldatetime = '2025-05-21 10:15:30';
SET @thedate = @thesmalldatetime;
SELECT @thesmalldatetime AS 'smalldatetime',
@thedate AS 'thedate';
Risultato:
+---------------------+------------+ | smalldatetime | thedate | |---------------------+------------| | 2025-05-21 10:16:00 | 2025-05-21 | +---------------------+------------+
Esempio 2: conversione esplicita con CAST()
DECLARE @thesmalldatetime smalldatetime;
SET @thesmalldatetime = '2025-05-21 10:15:30';
SELECT @thesmalldatetime AS 'thesmalldatetime',
CAST(@thesmalldatetime AS date) AS 'date';
Risultato:
+---------------------+------------+ | thesmalldatetime | date | |---------------------+------------| | 2025-05-21 10:16:00 | 2025-05-21 | +---------------------+------------+
Esempio 3: conversione esplicita con CONVERT()
DECLARE @thesmalldatetime smalldatetime;
SET @thesmalldatetime = '2025-05-21 10:15:30';
SELECT @thesmalldatetime AS 'thesmalldatetime',
CONVERT(date, @thesmalldatetime) AS 'date';
Risultato:
+---------------------+------------+ | thesmalldatetime | date | |---------------------+------------| | 2025-05-21 10:16:00 | 2025-05-21 | +---------------------+------------+