3 Ways to Reach First Day of Month in SQL Server
Hello to everyone,
In this article, I will try to give information about 3 ways to reach the first day of the month in SQL Server.
In SQL Server you may want to get the first day of the month in some cases.
You can easily do this using the code below.
Method 1
DECLARE @date date;
SET @date = '2023-10-15';
SELECT DATEADD(dd, -( DAY( @date ) -1 ), @date);
Method 2
DECLARE @date date;
SET @date = '2023-10-15';
SELECT DATEADD(month, DATEDIFF(month, 0, @date), 0);
Method 3
DECLARE @date DATETIME;
SET @date = '2023-10-15';
SELECT @date - DAY(@date) + 1;
When you run the above codes, you will see a result similar to the one below.
As you can see, we have learned 3 ways to reach the first day of the month.
Good luck to everyone in business and life.