Datetime-Wert zerlegen

Folgende Beschreibung hab ich im entwickler-forum.de gefunden.

Über die DATEPART-Funktion kann eine SELECT-Abfrage nur bestimmte Bestandteile zurückliefern.

 DECLARE @mydatetime DATETIME
 DECLARE @myms INT
 DECLARE @myss INT
 DECLARE @mymi INT
 DECLARE @myhh INT
 DECLARE @mydd INT
 DECLARE @mymm INT
 DECLARE @myyyyy INT
 
 SET @mydatetime=getdate()
 SET @myms = DATEPART(ms,@mydatetime)
 SET @myss = DATEPART(ss,@mydatetime)
 SET @mymi = DATEPART(mi,@mydatetime)
 SET @myhh = DATEPART(hh,@mydatetime)
 SET @mydd = DATEPART(dd, @mydatetime)
 SET @mymm=DATEPART(mm,@mydatetime)
 SET @myyyyy = DATEPART(yyyy, @mydatetime)
 
 SELECT @myyyyy AS 'Year', @mymm AS 'Month', @mydd AS 'Day of month',
        @myhh AS 'Hour', @mymi AS 'Minute', @myss AS 'Second',
        @myms AS 'Millisecond'

 

Veröffentlicht in MSSQL