開始使用免費開始

DECLARE 與 CAST

來使用 DECLARE()CAST(),把一個 date 變數和一個 time 變數合併成一個 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
編輯並執行程式碼