开始使用免费开始使用

DECLARE 与 CAST

请使用 DECLARE()CAST() 将一个日期变量与一个时间变量合并为一个 datetime 变量。

本练习是课程的一部分

SQL Server 中的函数与存储过程编写

查看课程

练习说明

  • 创建名为 @ShiftStartTimetime 变量,并将初始值设为 '08:00 AM'
  • 创建名为 @StartDatedate 变量,并将其设为 BikeShare 表中第一个 StartDate
  • 创建名为 `@ShiftStartDateTimedatetime 变量。
  • @StartDate@ShiftStartTime 转换为 datetime 数据类型,并赋值给 @ShiftStartDateTime

交互式实操练习

通过完成这段示例代码来试试这个练习。

-- Create @ShiftStartTime
___ ___ AS time = '08:00 AM'

-- Create @StartDate
___ ___ AS date

-- Set StartDate to the first StartDate from CapitalBikeShare
___ 
	___ = (
    	SELECT TOP 1 ___ 
    	FROM CapitalBikeShare 
    	ORDER BY StartDate ASC
		)

-- Create ShiftStartDateTime
___ ___ AS datetime

-- Cast StartDate and ShiftStartTime to datetime data types
___ @ShiftStartDateTime = ___(___ AS datetime) + ___(___ AS datetime) 

SELECT @ShiftStartDateTime
编辑并运行代码