In SQL Server kann ein datetime2-Wert in smalldatetime konvertiert werden. Das spart Speicher, reduziert aber die Genauigkeit.
datetime2 unterstützt eine Sekundenbruchteil-Präzision von 0 bis 7. smalldatetime hat keine Sekundenbruchteile und ist auf die nächste Minute genau.
Implizite Konvertierung
DECLARE @thedatetime2 datetime2, @thesmalldatetime smalldatetime; SET @thedatetime2 = '2025-05-21 10:15:30.1234567'; SET @thesmalldatetime = @thedatetime2; SELECT @thedatetime2 AS 'datetime2', @thesmalldatetime AS 'smalldatetime';
Ergebnis:
2025-05-21 10:15:30.1234567 2025-05-21 10:16:00
Die Sekunden werden auf 0 gesetzt und die Zeit auf die nächste Minute gerundet.
Explizite Konvertierung mit CAST
DECLARE @thedatetime2 datetime2(0); SET @thedatetime2 = '2025-05-21 10:15:30'; SELECT @thedatetime2 AS 'datetime2', CAST(@thedatetime2 AS smalldatetime) AS 'smalldatetime';
Explizite Konvertierung mit CONVERT
DECLARE @thedatetime2 datetime2(0); SET @thedatetime2 = '2025-05-21 10:15:30'; SELECT @thedatetime2 AS 'datetime2', CONVERT(smalldatetime, @thedatetime2) AS 'smalldatetime';