Date & Time Basics: YEAR, MONTH, DAY, DATEPART, DATENAME
Learn the DATE, TIME and DATETIME2 types, get the current time with GETDATE(), and extract parts with YEAR/MONTH/DAY, DATEPART and DATENAME.
"How many players debuted in 2008?", "Which month had the most runs?", "On which weekday was the match played?" — all of these need a part of a date. SQL Server's date & time functions answer them.
Definition — the three kinds of date/time values
A date/time data type stores a point in time in a special format that SQL understands, so it can extract parts, compare and calculate — something it cannot do with plain text.
| Type | Stores | Example | Format |
|---|---|---|---|
DATE | only the date | 2025-08-20 | year-month-day |
TIME | only the time of day | 18:55:45 | hours:minutes:seconds |
DATETIME2 (a timestamp) | date and time together | 2025-08-20 18:55:45.840 | e.g. a match start time |
DATETIME2 is SQL Server's name for what other databases call a TIMESTAMP. (SQL Server also has the older DATETIME; prefer DATETIME2 for new tables — it is more precise.) Always write date literals as 'YYYY-MM-DD' — this ISO format is understood the same way on every server setting.
GETDATE() — the current date and time
GETDATE() returns the current date and time of the database server as a DATETIME value. It takes no arguments and gives a different answer every time you run it — useful for "today", ages and "days since" calculations.
sql
SELECT GETDATE() AS right_now;The result is today's date and time, e.g. 2025-08-20 18:55:45.840 (yours will differ).
The four families of date & time functions
| Family | Functions | Purpose |
|---|---|---|
| Part extraction | DAY, MONTH, YEAR, DATEPART, DATENAME, DATETRUNC, EOMONTH | pull a piece out of a date |
| Format & casting | FORMAT, CONVERT, CAST | change how a date looks / its type |
| Calculations | DATEADD, DATEDIFF | add to dates, measure gaps |
| Validation | ISDATE | check whether a value is a real date |
Part-extraction overview
| Function | Returns | Return type |
|---|---|---|
DAY(date) | the day of the month | INT |
MONTH(date) | the month number (1–12) | INT |
YEAR(date) | the year | INT |
DATEPART(part, date) | any part as a number | INT |
DATENAME(part, date) | a part as text, e.g. 'August' | string |
DATETRUNC(part, date) | the date with lower parts reset | date/DATETIME2 |
EOMONTH(date) | the last day of the month | DATE |
Syntax
sql
YEAR(date) MONTH(date) DAY(date)
DATEPART(part, date) -- part: year, quarter, month, dayofyear, day, week, weekday, hour, minute, second
DATENAME(part, date)The part is written without quotes (DATEPART(month, debut)). Short forms also work: yy, qq, mm, dd, dw, hh, mi, ss.
YEAR, MONTH, DAY — the quick functions
YEAR, MONTH and DAY each extract one part of a date and return it as an INT. They are shortcuts for
DATEPART(year, …),DATEPART(month, …)andDATEPART(day, …).
sql
SELECT
YEAR('2025-08-20') AS y,
MONTH('2025-08-20') AS m,
DAY('2025-08-20') AS d;| y | m | d |
|---|---|---|
| 2025 | 8 | 20 |
DATEPART — any part as a number
DATEPART(part, date) returns the requested part of a date as an integer. It can return parts that have no shortcut function, such as quarter, week, weekday and hour.
sql
SELECT
DATEPART(year, '2025-08-20') AS yr,
DATEPART(month, '2025-08-20') AS mon,
DATEPART(quarter, '2025-08-20') AS qtr,
DATEPART(weekday, '2025-08-20') AS wkday;| yr | mon | qtr | wkday |
|---|---|---|---|
| 2025 | 8 | 3 | 4 |
Weekday: with SQL Server's default (US English) settings Sunday = 1, so Wednesday = 4. The first day of the week depends on the SET DATEFIRST setting, so don't hard-code weekday numbers across servers — use DATENAME when you need the day.
DATENAME — a part as text
DATENAME(part, date) returns the requested part as a string (text). For month and weekday it returns the name (
'August','Wednesday'); for numeric parts like year it returns the number as text ('2025').
sql
SELECT
DATENAME(month, '2025-08-20') AS month_name,
DATENAME(weekday, '2025-08-20') AS day_name,
DATENAME(year, '2025-08-20') AS year_text;| month_name | day_name | year_text |
|---|---|---|
| August | Wednesday | 2025 |
Perfect for readable reports. But because the result is text, sorting by it gives alphabetical order (April, August, December …) — sort by MONTH(date) instead.
Example — date functions in a query
sql
SELECT
name,
debut,
YEAR(debut) AS debut_year,
DATENAME(month, debut) AS month,
DATEDIFF(year, debut, '2025-08-20') AS years
FROM player_profiles;| name | debut | debut_year | month | years |
|---|---|---|---|---|
| Virat Kohli | 2008-04-20 | 2008 | April | 17 |
| Rohit Sharma | 2008-04-22 | 2008 | April | 17 |
| Jos Buttler | 2016-04-09 | 2016 | April | 9 |
| MS Dhoni | 2008-04-19 | 2008 | April | 17 |
| David Warner | 2009-04-18 | 2009 | April | 16 |
| Jasprit Bumrah | 2013-04-06 | 2013 | April | 12 |
How it works: YEAR gives an INT, DATENAME gives text, and DATEDIFF counts how many year boundaries were crossed between the debut and 2025-08-20 (covered in Date Calculations). The debut column lives in player_profiles, our version of the PDF's chapter-7 players table.
Common mistakes
- ❌
DATEPART('month', debut)with quotes → error in SQL Server. ✅DATEPART(month, debut). - ❌
ORDER BY DATENAME(month, d)expecting calendar order. ✅ORDER BY MONTH(d). - ❌
WHERE YEAR(debut) = 2008on a huge table — it works, but wrapping the column in a function stops SQL Server from using an index. ✅ Prefer a range:WHERE debut >= '2008-01-01' AND debut < '2009-01-01'. - ❌ Writing dates as
'20/08/2025'— it may be misread depending on language settings. ✅ Use'2025-08-20'.
SQL Server vs MySQL / PostgreSQL
| Task | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| Now | GETDATE() / SYSDATETIME() | NOW() | NOW() / CURRENT_TIMESTAMP |
| Any part | DATEPART(month, d) | EXTRACT(MONTH FROM d) | EXTRACT(MONTH FROM d) / DATE_PART('month', d) |
| Month name | DATENAME(month, d) | MONTHNAME(d) | TO_CHAR(d, 'Month') |
Interview tip
Q: What is the difference between DATEPART and DATENAME?
Both extract a part of a date. DATEPART returns an INT (DATEPART(month, '2025-08-20') = 8), DATENAME returns a string ('August'). Use DATEPART for calculations and sorting, DATENAME for display.
Key points to remember
- DATE stores a date, TIME stores a time of day, DATETIME2 (a timestamp) stores both together.
- GETDATE() returns the current date and time of the server; it takes no arguments.
- YEAR, MONTH and DAY each return one part of a date as an INT.
- DATEPART(part, date) returns ANY part (quarter, week, weekday, hour …) as an INT; the part is written without quotes.
- With default US settings DATEPART(weekday, …) counts Sunday = 1, so Wednesday = 4.
- DATENAME(part, date) returns the part as TEXT ('August', 'Wednesday') — great for display, bad for sorting.
- Write date literals in ISO format 'YYYY-MM-DD'.
Try it yourself
YEAR, MONTH, DAY, DATEPART and DATENAME
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Debut details for every player
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
What time is it on the server?
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: players who debuted in 2008
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
