In this article, I founda sql query to find first, last and total days in a month from the current orany date using inbuilt functions such as DATEADD(),DATEDIFF() and DAYS() functions.Here i am using current date. If you want to get any date then replace your date with GETDATE()
DECLARE
@CurrentDate DATE=GETDATE(),
@FirstDate DATE,
@LastDate DATE,
@TotalDays INT
SET @FirstDate=(SELECT DATEADD(dd,-DAY(@CurrentDate)+1,@CurrentDate))
SET @LastDate=
(SELECT DATEADD(dd,-DAY(DATEADD(mm,1,@CurrentDate)),
DATEADD(mm,1,@CurrentDate)))
SET @TotalDays=(DATEDIFF(D,@FirstDate,@LastDate))+1
SELECT @FirstDate[FirstDate of Month] ,@LastDate [LastDate ofMonth], @TotalDays [TotalDays in Month]
Output
--------
FirstDate of Month LastDate of Month TotalDaysin Month
2016-03-01 2016-03-31 31
Post your comments / questions
Recent Article
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
- How to get thumbnail from vimeo video URL in Python?
Related Article