Finding Last Day of the Month IN SQL Server

Certain applications require business logic to determine the last day of the month. This is the case for accounting applications that perform such tasks as invoice processing and customer billing. After seeing this logic implemented in various means, even as simple as harcoding the last day of each month, I developed a simple function that does the same:

 DECLARE @inDate   	AS DATETIMEDECLARE @tempDate 	AS DATETIMEDECLARE @lastDayOfMonth AS DATETIME/*incoming date to use to determine the last day of the month*/SET @inDate = '1/1/2000' /* take the date and add one month */SET @tempDate = DateAdd(MONTH, 1 , @inDate)/* create a date MM/01/YYYY and subtract 1 day so that    it returns the last day of the month*/SET @lastDayOfMonth = DateAdd(DAY, -1 , 			CONVERT(DATETIME, CONVERT(char(2), MONTH(@tempDate)) + 			'/01/' +  			CONVERT(char(4), YEAR(@tempDate))))PRINT 'Last Day of ' + DATENAME(month, @inDate) +       ' is ' + CONVERT(VARCHAR, @lastDayOfMonth)


Therefore the above T-SQL script uses January 1, 2000 and determine the last day of January is the 31st. The output is:

 Last Day of January is Jan 31 2000 12:00AM
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS