从各部分构建日期和时间
SQL Server 提供了若干函数,可根据各个部分生成日期与时间的组合。在本练习中,我们将使用 DATETIME2FROMPARTS() 和 DATETIMEFROMPARTS()。
Neil Armstrong 和 Buzz Aldrin 驾驶阿波罗 11 号登月舱(昵称 The Eagle)于 1969 年 7 月 20 日 20:17(UTC)在月球着陆。他们在月面停留约 21 又 1/2 小时,并于 1969 年 7 月 21 日 18:54(UTC)起飞。
本练习是课程的一部分
SQL Server 中的时间序列分析
练习说明
- 使用
DATETIME2FROMPARTS()构建 Neil 和 Buzz 成为首批登月者时的日期与时间。注意DATETIME2FROMPARTS()中的 "2",表示我们要构建的是DATETIME2而不是DATETIME。 - 使用
DATETIMEFROMPARTS()构建他们从月球起飞时的日期与时间。请注意这里构建的是DATETIME,而非DATETIME2。
交互式实操练习
通过完成这段示例代码来试试这个练习。
SELECT
-- Mark the date and time the lunar module touched down
-- Use 24-hour notation for hours, so e.g., 9 PM is 21
DATETIME2FROMPARTS(___, ___, ___, ___, ___, 00, 000, 0) AS TheEagleHasLanded,
-- Mark the date and time the lunar module took back off
-- Use 24-hour notation for hours, so e.g., 9 PM is 21
DATETIMEFROMPARTS(___, ___, ___, ___, ___, 00, 000) AS MoonDeparture;