Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a datetime from year, day of the year and hour in SQL Server 2008 R2

I have a table in a Microsft SQL Server 2008 R2, which came from an outside source. The columns of the are as follows: ID, Year, DAY, HOUR & Value, where DAY contains the day of the year (from 1 to 366) and HOUR represents the hour of the day (from 0 to 23).

I wish to create a new datetime column and populate it with the dateTime created from the data in Year, DAY & HOUR columns.

What SQL function should I use to create the DateTime from its parts?

SQL Server 2012 has DATETIMEFROMPARTS, but there is no equivalent function for SQL Server 2008 R2

like image 627
Devdatta Tengshe Avatar asked Apr 30 '13 11:04

Devdatta Tengshe


People also ask

How can create date from day month and year in SQL?

SQL Server DATEFROMPARTS() Function The DATEFROMPARTS() function returns a date from the specified parts (year, month, and day values).


1 Answers

declare @Year int = 2003
declare @Day int = 100
declare @Hour int = 13

select dateadd(hour, @Hour, dateadd(dayofyear, @Day - 1, dateadd(year, @Year - 1900, 0)))
like image 122
Mikael Eriksson Avatar answered Oct 17 '22 04:10

Mikael Eriksson