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
SQL Server DATEFROMPARTS() Function The DATEFROMPARTS() function returns a date from the specified parts (year, month, and day values).
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With