Search⌘ K
AI Features

Answer: Extract a Value From a Date

Explore how to extract the year from date values in SQL tables using functions like YEAR, SUBSTRING, LEFT, and DATE_FORMAT. Understand the use of aliases and SELECT queries to retrieve and format date parts effectively. This lesson equips you to handle common date extraction tasks in SQL interviews and data queries.

Solution

The solution is given below:

MySQL
/* The query to extract the year from a date */
SELECT *, YEAR(DateOfJoining) AS YearOfJoining
FROM EmployeeDetails;

Explanation

The explanation of the code solution is given below:

  • Line 2: The SELECT query selects all the columns using * and the calculated column with the date/time function YEAR to find the year. We use AS to set an alias for the column.

  • Line 3: The FROM clause specifies the table name as EmployeeDetails. ...

Recall of relevant concepts