Il existe 3 méthodes différentes en fonction de mes besoins et de la version que j'utilise.
Voici les méthodes.
1) Utilisation de Convert
DECLARE @DateTime DATETIME = GETDATE();
--Using Convert
SELECT
CONVERT(NVARCHAR, @DateTime,120) AS 'myDateTime'
,CONVERT(NVARCHAR(10), @DateTime, 120) AS 'myDate'
,RIGHT(CONVERT(NVARCHAR, @DateTime, 120),8) AS 'myTime'
2) Utilisation de Cast (SQL Server 2008 et au-delà)
SELECT
CAST(@DateTime AS DATETIME2) AS 'myDateTime'
,CAST(@DateTime AS DATETIME2(3)) AS 'myDateTimeWithPrecision'
,CAST(@DateTime AS DATE) AS 'myDate'
,CAST(@DateTime AS TIME) AS 'myTime'
,CAST(@DateTime AS TIME(3)) AS 'myTimeWithPrecision'
3) Utilisation du type de données caractère de longueur fixe
DECLARE @myDateTime NVARCHAR(20) = CONVERT(NVARCHAR, @DateTime, 120);
DECLARE @myDate NVARCHAR(10) = CONVERT(NVARCHAR, @DateTime, 120);
SELECT
@myDateTime AS 'myDateTime'
,@myDate AS 'myDate'