T-SQL reference › DATEPART

DATEPART and DATENAME in SQL Server

DATEPART pulls a number out of a date (month = 10). DATENAME pulls a word (October). Weekday numbers depend on DATEFIRST, which is not the same on every server.

Month and weekday

SELECT order_id, order_date,
       DATEPART(month, order_date) AS month_no,
       DATENAME(month, order_date) AS month_name,
       DATENAME(weekday, order_date) AS weekday_name
FROM Orders;

For “Monday to Sunday last week”, do not trust DATEPART(weekday, …) alone. Pin the week as on last week’s orders.

Try it on Harbour & Co

James Rowe (Sales Manager) needs this now: “Need last week’s orders over £50 for the 10am. Don’t include today. Newest first.” Write the query, run it, send the results. Free.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets