Get Started

Creating a date from parts

While most functions you worked with so far extract parts from a date, DATEFROMPARTS() does exactly the opposite: it creates a date from three numbers, representing the year, month and the day.

The syntax is: DATEFROMPARTS(year, month, day)

You can also use expressions that return numeric values as parameters for this function, like this: DATEFROMPARTS(YEAR(date_expression), MONTH(date_expression), 2)

In this exercise, you will select information from the voters table, including the year and the month of the first_vote_date. Then, you will create a new date column representing the first day in the month of the first vote.

This is a part of the course

“Functions for Manipulating Data in SQL Server”

View Course

Exercise instructions

  • Select the year of the first vote.
  • Select the month of the first vote date.
  • Create a date as the start of the month of the first vote.

Hands-on interactive exercise

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

SELECT 
	first_name,
	last_name,
    -- Select the year of the first vote
   	___(first_vote_date) AS first_vote_year, 
    -- Select the month of the first vote
	___(first_vote_date) AS first_vote_month,
    -- Create a date as the start of the month of the first vote
	___(___, ___, 1) AS first_vote_starting_month
FROM voters;

This exercise is part of the course

Functions for Manipulating Data in SQL Server

IntermediateSkill Level
4.7+
7 reviews

Learn the most important functions for manipulating, processing, and transforming data in SQL Server.

Date and time functions are an important topic for databases. In this chapter, you will get familiar with the most common functions for date and time manipulation. You will learn how to retrieve the current date, only parts from a date, to assemble a date from pieces and to check if an expression is a valid date or not.

Exercise 1: Functions that return system date and timeExercise 2: Get the know the system date and time functionsExercise 3: Selecting parts of the system's date and timeExercise 4: Functions returning date and time partsExercise 5: Extracting parts from a dateExercise 6: Generating descriptive date partsExercise 7: Presenting parts of a dateExercise 8: Creating a date from parts
Exercise 9: Performing arithmetic operations on datesExercise 10: Arithmetic operations with datesExercise 11: Modifying the value of a dateExercise 12: Calculating the difference between datesExercise 13: Validating if an expression is a dateExercise 14: Changing the date formatExercise 15: Changing the default languageExercise 16: Correctly applying different date functions

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free