Get startedGet started for free

DECLARE & CAST

Let's use DECLARE() and CAST() to combine a date variable and a time variable into a datetime variable.

This exercise is part of the course

Writing Functions and Stored Procedures in SQL Server

View Course

Exercise instructions

  • Create a time variable named @ShiftStartTime and set initial value to '08:00 AM'.
  • Create a date variable named @StartDate and set it to the first StartDate from the BikeShare table.
  • Create a datetime variable named `@ShiftStartDateTime.
  • Change @StartDate and @ShiftStartTime to datetime data types and assign to @ShiftStartDateTime.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

-- 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
Edit and Run Code