How to Get First Day of Month in SQL Server: 5 Methods

A SaaS company asked me to look at a billing dashboard that took 40 seconds to load. Every query grouped invoices by month. The developer had converted each date to text, cut off the day, and converted it back. On a table with 80 million rows, that approach was slow, and it also broke the moment the server’s language settings changed.

Reports like this depend on one small skill: how to get first day of month in SQL. Monthly billing, cohort analysis, and partitioned archives all start with the month’s first date. SQL Server has several ways to do it, and they differ in readability, version support, and index friendliness.

You will learn five reliable methods, when to use each, and how to keep month filters fast.

How to Get First Day of Month in SQL Server

SQL Server has no built-in FIRSTDAY() function. You build the result from date functions. Pick your method based on your SQL Server version and what the query does with the result.

MethodMinimum versionReturnsBest for
DATETRUNCSQL Server 2022, Azure SQLInput typeNew code, clean syntax
DATEFROMPARTSSQL Server 2012dateReadability
EOMONTH + DATEADDSQL Server 2012dateReadable, version-safe
DATEADD + DATEDIFFAll supported versionsdatetime or dateMaximum compatibility
CAST/FORMAT to textAvoidvariesNot recommended

Before choosing, understand the types. A date stores only the day. A datetime2 stores date and time. Method results differ by type, so read about SQL Server date data types and date vs. datetime if you are unsure.

Pro Tip: In my experience, I keep a short “date cookbook” script in the team repository. When every developer copies the same tested snippet, the month-start logic stays consistent across reports.

Method 1: DATETRUNC (SQL Server 2022 and Later)

DATETRUNC truncates a date to the unit you choose. It is the cleanest option, and Microsoft’s own EOMONTH documentation points to it as the way to find the start of a month on SQL Server 2022 and later.

SELECT DATETRUNC(month, SYSUTCDATETIME()) AS FirstDayOfMonth;

This returns the first moment of the current UTC month. month is the datepart, and the second argument is the date to truncate. The result keeps the input’s data type, so a datetime2 input returns a datetime2 at midnight on the first.

Use it when your platform supports it. Azure SQL Database includes it, but a SQL Server 2019 instance does not. Check your compatibility level before depending on it. The article on the DATETRUNC function covers more dateparts.

Pro Tip: I check SELECT @@VERSION and the database compatibility level before using any new T-SQL function. A migration to a newer engine can pass a test and still fail on an older reporting replica.

Method 2: DATEFROMPARTS

DATEFROMPARTS builds a date from year, month, and day numbers. You set the day to 1.

DECLARE @OrderDate datetime2(0) = '2026-11-26 14:35:10';

SELECT DATEFROMPARTS(YEAR(@OrderDate), MONTH(@OrderDate), 1) AS FirstDayOfMonth;

The query extracts the year and month, then constructs a new date on day 1. The result is 2026-11-01 as a date type. After running the query above, I got the expected output shown in the screenshot below.

sql get first day of month

The function raises an error for invalid arguments, which is good. Bad data fails loudly instead of producing a wrong result silently. Learn the helpers it uses in the guides for the YEAR function and MONTH function.

Pro Tip: I prefer DATEFROMPARTS in stored procedures other people will read. Six months later, nobody has to decode what a clever formula was meant to do.

Method 3: EOMONTH Plus DATEADD

EOMONTH returns the last day of a month and accepts an offset. If you take the last day of the previous month and add one day, you land on the first day of the current month.

SELECT DATEADD(day, 1, EOMONTH(SYSUTCDATETIME(), -1)) AS FirstDayOfMonth;

EOMONTH(..., -1) goes back one month and returns that month’s final day. DATEADD(day, 1, ...) moves forward to the first. This works on SQL Server 2012 and later, and it handles different month lengths and leap years for you.

After running the query above, I got the expected output shown in the screenshot below.

sql get first day of month from date

The same trick gives you the first day of next month, which is useful for half-open date ranges:

SELECT DATEADD(day, 1, EOMONTH(SYSUTCDATETIME())) AS FirstDayOfNextMonth;

After running the query above, I got the expected output shown in the screenshot below.

ms sql get first day of month

Read more in the EOMONTH function guide and the DATEADD function guide.

Pro Tip: I use first day of next month as the upper bound in filters. That habit removes the old “23:59:59.997” workaround, which fails on datetime2 columns with higher precision.

Method 4: DATEADD With DATEDIFF

This is the classic pattern, and it works everywhere. It counts the months between a base date and your date, then adds those months back to the base.

SELECT DATEADD(month, DATEDIFF(month, 0, SYSUTCDATETIME()), 0) AS FirstDayOfMonth;

The base value 0 converts to 1900-01-01. DATEDIFF counts the month boundaries from 1900 to today. DATEADD adds that many months back to 1900-01-01, which lands exactly on the first of the current month at midnight.After running the query above, I got the expected output shown in the screenshot below.

how to get first day of month in sql

The trade-off is readability. Newer developers often stare at the zeros. It also returns datetime by default when you pass a literal zero, so cast it when you need a date:

SELECT CAST(DATEADD(month, DATEDIFF(month, 0, SYSUTCDATETIME()), 0) AS date) AS FirstDayOfMonth;

After running the query above, I got the expected output shown in the screenshot below.

how to get first day of the month in sql

Understanding why this works means understanding that DATEDIFF counts calendar boundaries, not elapsed time. Date math in SQL Server explains the idea in more depth.

Pro Tip: I add a comment next to this pattern explaining that zero means 1900-01-01. That one comment has saved several code reviews.

What to Avoid: Converting Dates to Text

Many old snippets use CONVERT or FORMAT to cut off the day and then convert back. I strongly advise against this for three reasons:

  • Slow: String conversion runs on every row. The FORMAT function is especially expensive at scale.
  • Language-sensitive: Parsing text dates can break when language or DATEFORMAT settings change. See the failure described in conversion failed from character string.
  • Wasteful: Text results cannot use a date index properly.

If you must produce text for display, format at the very last step, and leave the calculations as real date types. The date formatting guide shows safe patterns.

Using Month Start in Real Reports

Let’s use an invoice scenario. The schema is a named container for objects, and here it is dbo.

CREATE TABLE dbo.Invoices
(
InvoiceId int IDENTITY(1,1) CONSTRAINT PK_Invoices PRIMARY KEY,
TenantId int NOT NULL,
InvoicedAtUtc datetime2(0) NOT NULL,
AmountUsd decimal(12,2) NOT NULL
);

This creates an invoice table with a primary key, which uniquely identifies each row. More patterns are in create table with primary key.

Group Revenue by Month

SELECT DATEFROMPARTS(YEAR(InvoicedAtUtc), MONTH(InvoicedAtUtc), 1) AS MonthStart,
SUM(AmountUsd) AS TotalRevenue
FROM dbo.Invoices
GROUP BY DATEFROMPARTS(YEAR(InvoicedAtUtc), MONTH(InvoicedAtUtc), 1)
ORDER BY MonthStart;

This labels each invoice with its month’s first day and sums revenue per month. Using a real date as the label keeps the output sortable. A string such as “Nov 2026” sorts alphabetically, which puts April before January. See also grouping by date.

Filter the Current Month Quickly

DECLARE @MonthStart date = DATEFROMPARTS(YEAR(SYSUTCDATETIME()), MONTH(SYSUTCDATETIME()), 1);
DECLARE @NextMonthStart date = DATEADD(month, 1, @MonthStart);

SELECT InvoiceId, AmountUsd
FROM dbo.Invoices
WHERE InvoicedAtUtc >= @MonthStart
AND InvoicedAtUtc < @NextMonthStart;

The code calculates both boundaries once, then compares the raw column to them. This is sargable, meaning SQL Server can use an index seek. Avoid wrapping the column, as in WHERE MONTH(InvoicedAtUtc) = 11, because that forces a scan of every row.

Support this filter with an index. A nonclustered index is a separate sorted structure that speeds lookups on chosen columns:

CREATE NONCLUSTERED INDEX IX_Invoices_InvoicedAtUtc
ON dbo.Invoices (InvoicedAtUtc)
INCLUDE (TenantId, AmountUsd);

The key column supports the range seek. The included columns let the query return results without extra lookups. Each added index slows inserts slightly, so confirm the need first with how to create an index and the index best practices. Also keep statistics current, as described in updating statistics.

Pro Tip: I look at the actual execution plan before and after a rewrite. If the plan still shows a scan on the date column, a function is hiding somewhere in the predicate.

Precompute for Heavy Reporting

If a dashboard groups by month on huge tables, add a persisted computed column:

ALTER TABLE dbo.Invoices
ADD InvoiceMonth AS DATEFROMPARTS(YEAR(InvoicedAtUtc), MONTH(InvoicedAtUtc), 1) PERSISTED;

Persisted means SQL Server stores the computed value on disk, so you can index it. This adds storage and write cost, which is the trade-off. This statement alters the table and may take a lock on large tables, so run it in a maintenance window. Review altering a table first

Troubleshooting Common Problems

  • Result has a time part: DATEADD with DATEDIFF returns datetime. Cast the result to date, or use DATEFROMPARTS.
  • DATETRUNC is not a recognized function: Your engine is older than SQL Server 2022. Use DATEFROMPARTS or EOMONTH.
  • Conversion errors on text dates: Convert with an explicit style. See converting varchar to date.
  • Wrong month near midnight: You mixed UTC and local time. Calculate boundaries in the same time standard that you store.
  • Slow month filters: A function wraps the column. Rewrite the predicate to compare the column against precomputed boundaries.

Frequently Asked Questions

What is the easiest way to get the first day of the month in SQL Server?

On SQL Server 2022 or Azure SQL, use DATETRUNC(month, date). On older versions, DATEFROMPARTS(YEAR(d), MONTH(d), 1) is the most readable choice.

How do I get the first day of the previous month?

Subtract one month first. For example, DATEFROMPARTS(YEAR(DATEADD(month, -1, d)), MONTH(DATEADD(month, -1, d)), 1) works on any version from 2012. You can also use DATEADD(day, 1, EOMONTH(d, -2)).

Does the DATEADD with DATEDIFF method still work?

Yes. It works on every supported version and returns midnight on the first day. It is harder to read, so add a comment and cast to date if you need to drop the time.

How do I get the last day of the month?

Use EOMONTH(date), which returns the last day as a date type. Add an offset to move to other months, such as EOMONTH(date, 1) for next month.

How do I speed up queries that group by month?

Filter with sargable date ranges, add an index on the date column, and consider a persisted computed month column. Then verify the improvement in the execution plan or Query Store.

You May Also Like

You learned five ways to find the first day of a month in SQL Server, plus how to use them in fast, index-friendly reports. Always compare your date column against precomputed boundaries instead of wrapping it in functions. I hope you found this article helpful.