DATEPART() Function in SQL

The DATEPART in SQL is a function that takes two inputs: a 'part of the date' such as year, month, or day and a 'date', and it returns that specific part of the date. The datepart in SQL is used to filter dates based on a certain attribute. This is mainly used in cases where we have multiple dates stored in our database and we want to filter queries based on a particular month.

Syntax
The datepart in SQL takes two arguments:
- datePart: The datePart refers to the part of the date that has to be found.
- inputDate: The inputDate refers to the date that we are inputting whose part we will display.
Valid inputs for datePart:
| datePart | abbreviation |
|---|---|
| year | yy, yyyy |
| quarter | qq, q |
| month | mm, m |
| dayofyear | dy, y |
| day | dd, d |
| week | wk, ww |
| hour | hh |
| minute | mi, n |
| second | ss, s |
| millisecond | ms |
| microsecond | mcs |
| nanosecond | ns |
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Examples for Datepart in SQL
Example 1: Displaying the day from the given Date
Code:
Output:
Explanation of the example:
In the above example, we have passed the date 2022-04-22 to the datepart function in SQL, and the value of datePart is day. Thus the datepart in SQL outputs 22.
Example 2: Displaying the Month from the given Date
Code:
Output:
Explanation of the example:
In the above example, we have passed the date 2022-04-22 to the datepart function in SQL, and the value of datePart is month. Thus the datepart in SQL outputs 04.
Turn Learning into Career Growth
Example 3: Displaying the year from the given Date
Code:
Output:
Explanation of the example:
In the above example, we have passed the date 2022-04-22 to the datepart function in SQL, and the value of datePart is year. Thus the datepart in SQL outputs 2022.
Example 4: Displaying the time from the given Date
Code:
Output:
Explanation of the example:
In the above example, we have passed the date 2022-04-22 to the datepart function in SQL, and the value of datePart is minute. Thus the datepart in SQL outputs 5.
Learn more about Advanced Concepts of SQL
In order to learn more about advanced concepts of SQL visit this link.
Conclusion
- The datepart in SQL returns a specific part of the date.
- The datepart in SQL takes two parameters.
- The datepart in SQL takes a date whose specific part is to be returned.
- The datepart in SQL also takes the denotation for the part to be returned (i.e. day, month, year, minute, second, hour etc).