Potato Salad
Potato Salad

Reputation: 4650

Splitting a string of datetime into different columns

I have this line:

,DATEADD(s, C.[Timestamp], CONVERT(DATETIME, '1-1-1970 00:00:00')) As "Timestamp"

Where timestamp is parsed into datetime format.

The output is:

2017-04-19 19:40:20.000

Which is nice, but I want to split my date and time into multiple columns. Like YYYY, MM, DD, HH so on.

Anyone have any idea?

PS: Pure sql. SQL Server 2016

Upvotes: 0

Views: 72

Answers (1)

RoMEoMusTDiE
RoMEoMusTDiE

Reputation: 4824

Use DATEPART

https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql

SELECT DATEPART(year, '12:10:30.123')  
    ,DATEPART(month, '12:10:30.123')  
    ,DATEPART(day, '12:10:30.123')  
    ,DATEPART(dayofyear, '12:10:30.123')  
    ,DATEPART(weekday, '12:10:30.123'); 

Upvotes: 2

Related Questions