Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dealing with dates
Working with time data
Date and time date
Extracting dates and times
Doing arithmetic with dates
Converting date and time data
Building date and time data from parts
46. The DATEFROMPARTS() function
Summary and review

Instruction

SQL Server allows you to build dates and times from "parts"—i.e., from partial information. These function are very useful if you have only integers indicating part of a date or time and you would like to get the full date or time.

The DATEFROMPARTS() function generates a date from integers given as arguments. It looks like this:

DATEFROMPARTS(year, month, day)

The three arguments are integers representing the year, month (from 1 to 12), and day. The arguments are always in the year-month-day order. If you write:

SELECT
  DATEFROMPARTS(2018, 7, 5);

it will return the date 2018-07-05.

Exercise

You want the current date, and only the date—no time info. Show how you'd use GETDATE() and DATEFROMPARTS() to create the current date from the year, month, and day parts. Name the column CurrentDate.

Note: you can use GETDATE() as an argument to return current date and time info. For example: YEAR(GETDATE()).

Stuck? Here's a hint!

You'll need to use the GETDATE() function to provide year, month, and day for the DATEFROMPARTS() function.