How to find the first day for the current month?
How to find the first day for the current month, last day of the current month, first day of next month, last day of next month, first day of previous month, last day of current month, first day of current year, and last day of current year?
These functions are deployed in process/reports, SSIS ELTS to retrieve the information as per the dates given. The functions are given below with logical explanation:
First day of current month:
Select DATEADD (mm, DATEDIFF(m,0,GETDATE()),0): Here we have calculated the difference between the months starting from 0 to present date and then summed up the difference in 0. This will give the first day of the ongoing month.
Last day of current month:
SELECT cast (DATEADD(s,-1,DATEADD (mm, DATEDIFF(m,0,GETDATE())+1,0)) as date): beginning from innermost query (DATEDIFF(m,0,GETDATE())) will support you in calculating the difference between the months from 0 to current date. Then we will include a month which will turn it into next month. Then we must subtract 1 sec from it and this will provide the last second of the month. Then we will turn that into a date to extract the last day of the current month.
First day of next month:
SELECT DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0) ): Here we have calculated the difference between the months from 0 to present date and then include 1 to the difference and 0. This will lead to returning the initial day of next month.
Last day of next month:
SELECT cast(DATEADD (s,-1, DATEADD(mm,DATEDIFF(m,0,GETDATE())+2,0)) as date)
First day of previous month:
Select DATEADD(mm,DATEDIFF(m,0,GETDATE())-1.0)
Last day of previous month:
SELECT cast(DATEADD(s,-1,DATEADD(mm,DATEDIFF(m,0,GETDATE()),0))as date)
The SQL Server Online Training offered at JanBask Training gives an experience like offline classes and prepares the students by covering the core concepts in MS SQL server certification making them ready to face the competitive job market. The students are also saved from the burden of traveling to physical locations to take classes. JanBask Training also aims to teach the wise methods of how the databases are generated and controlled in the business.