从 SQL Server 2008 R2 中的年、年和小时获取日期时间 [英] Get a datetime from year, day of the year and hour in SQL Server 2008 R2

查看:19
本文介绍了从 SQL Server 2008 R2 中的年、年和小时获取日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Microsft SQL Server 2008 R2 中有一个表,它来自外部来源.的列如下:ID、Year、DAY、HOUR &值,其中 DAY 包含一年中的第几天(从 1 到 366),HOUR 代表一天中的小时(从 0 到 23).

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).

我希望创建一个新的 datetime 列,并用从 Year, DAY & 中的数据创建的 dateTime 填充它HOUR 列.

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

我应该使用什么 SQL 函数从其部分创建 DateTime?

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

SQL Server 2012 具有 DATETIMEFROMPARTS,但对于 SQL Server 2008 没有等效功能R2

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

推荐答案

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)))

这篇关于从 SQL Server 2008 R2 中的年、年和小时获取日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆