Only this week, get the SQL Complete Track of 9 courses in a special prize of $330 $89!
Dealing with dates
Working with time data
Date and time date
Extracting dates and times
Doing arithmetic with dates
Converting date and time data
33. The CAST() function
Building date and time data from parts
Summary and review


Previously in this course, you learned about T-SQL conversion functions. You can also use these functions with date and time data. Let's see how.

First up is the CAST() function. It works with date and time data the same way as with other data: you use it to change one data type into another type. Look at the query below:

  CAST(LaunchedDatetime AS TIME) AS LaunchedDateTime
FROM Aircraft
WHERE Id = 1;

The LaunchedDatetime column field where Id = 1 contains a DATETIMEOFFSET value:

2014-06-10 07:55:00.0000000 +00:00

If you convert this into a TIME data type, it will be displayed as 07:55:00.0000000.


Show the DiscontinuationDatetime column as a DATE for all discontinued aircraft. Do not include time data. Name the column DiscontinuationDatetime.

Stuck? Here's a hint!

Use CAST(DiscontinuationDatetime AS DATE). All the discontinued aircraft do not have NULL in DiscontinuationDatetime column.