Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Find First/Last Day Of Month For Given Date

Posted on August 7, 2013October 26, 2015 By David Kittell
DECLARE @entry_date DATETIME
	,@start_date DATETIME
	,@end_date DATETIME

SET @entry_date = '7-1-2013'
-- Set Start Date to first day of the month for given date
SET @start_date = (
		SELECT CONVERT(VARCHAR(25), DATEADD(dd, - (DAY(@entry_date) - 1), @entry_date), 101)
		)
-- Set End Date to last day of the month for given date
SET @end_date = (
		SELECT CONVERT(VARCHAR(25), DATEADD(dd, - (DAY(DATEADD(mm, 1, @entry_date))), DATEADD(mm, 1, @entry_date)), 101)
		)
----Last Day of Previous Month
SELECT DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)) LastDay_PreviousMonth

----Last Day of Current Month
SELECT DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0)) LastDay_CurrentMonth

----Last Day of Next Month
SELECT DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 2, 0)) LastDay_NextMonth

--Last Day of Any Month and Year
DECLARE @dtDate DATETIME

SET @dtDate = '8/18/2007'

SELECT DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, @dtDate) + 1, 0)) LastDay_AnyMonth
LastDay_PreviousMonth
-----------------------
2013-07-31 23:59:59.000

LastDay_CurrentMonth
-----------------------
2013-08-31 23:59:59.000

LastDay_NextMonth
-----------------------
2013-09-30 23:59:59.000

LastDay_AnyMonth
-----------------------
2007-08-31 23:59:59.000
DECLARE @mydate DATETIME

SELECT @mydate = GETDATE()

SELECT CONVERT(VARCHAR(25), DATEADD(dd, - (DAY(@mydate)), @mydate), 101)
	,'Last Day of Previous Month'

UNION

SELECT CONVERT(VARCHAR(25), DATEADD(dd, - (DAY(@mydate) - 1), @mydate), 101) AS Date_Value
	,'First Day of Current Month' AS Date_Type

UNION

SELECT CONVERT(VARCHAR(25), @mydate, 101) AS Date_Value
	,'Today' AS Date_Type

UNION

SELECT CONVERT(VARCHAR(25), DATEADD(dd, - (DAY(DATEADD(mm, 1, @mydate))), DATEADD(mm, 1, @mydate)), 101)
	,'Last Day of Current Month'

UNION

SELECT CONVERT(VARCHAR(25), DATEADD(dd, - (DAY(DATEADD(mm, 1, @mydate)) - 1), DATEADD(mm, 1, @mydate)), 101)
	,'First Day of Next Month'
GO
07/31/2013                Last Day of Previous Month
08/01/2013                First Day of Current Month
08/07/2013                Today
08/31/2013                Last Day of Current Month
09/01/2013                First Day of Next Month

Source:

  1. http://blog.sqlauthority.com/2007/05/13/sql-server-query-to-find-first-and-last-day-of-current-month/
  2. http://blog.sqlauthority.com/2007/08/18/sql-server-find-last-day-of-any-month-current-previous-next/
Originally Posted on August 7, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MSSQL

Post navigation

Previous post
Next post

Related Posts

Page URL With Changing Variables

Posted on June 28, 2013October 26, 2015

Often I’ve had to build a page that didn’t allow the datagrid or listview built in sort options so I came up with this function so that you can keep filters and change the sorting and direction of the page. public string sPageURLwVariables(string sArrow, string sDir) { string sOriginalURL =…

Read More

Last Read / Last Write

Posted on April 29, 2013October 26, 2015

Sometimes you need to know the last time a table or database was read from or written to, this code will help determine that for you. Originally Found At: http://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/ USE <Database_name> SET ANSI_WARNINGS OFF; SET NOCOUNT ON; GO WITH agg AS ( SELECT last_user_seek, last_user_scan, last_user_lookup, last_user_update FROM sys.dm_db_index_usage_stats…

Read More

Mac OSX Terminal – Wi-Fi & Ethernet Switch

Posted on April 9, 2018

Sometimes you need to turn off Wi-Fi and turn on Ethernet and vice-versa NOTE: Important to know what your configuration is. sudo ifconfig en6 up && sudo ifconfig en0 down sudo ifconfig en6 down && sudo ifconfig en0 up All information on this site is shared with the intention to…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes