Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 888739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:26:56+00:00 2026-05-15T13:26:56+00:00

I am writing a function in T-SQL returning the last day of the month

  • 0

I am writing a function in T-SQL returning the last day of the month regardless of the date input.

Here is my code:

Alter Function dbo.FN_Get_Last_Day_in_Month2
(@FN_InputDt    Datetime)
Returns smalldatetime
as 
Begin

Declare @Result  smalldatetime
Set    @Result =

       case when @FN_InputDt <> 01-01-1900 then 
       DATEADD(m, DATEDIFF(M, 0,@FN_InputDt)+1, -1)
       Else 0 End

Return @Result

End 

The code is not working correctly, here is a test that shows the bad behavior:

SELECT dbo.fn_get_last_day_in_month (07-05-2010)

Here is the (incorrect) result:

2010-07-31 00:00:00
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:26:57+00:00Added an answer on May 15, 2026 at 1:26 pm

    What is 07-05-2010…May 7th or July 5th? You need to use a safe date format, take a look at Setting a standard DateFormat for SQL Server

    example from How to find the first and last days in years, months etc

    DECLARE @d DATETIME
    SET @d = '20100705'  -- notice ISO format
    
    SELECT
        DATEADD(yy, DATEDIFF(yy, 0, @d), 0) AS FirstDayOfYear,
        DATEADD(yy, DATEDIFF(yy, 0, @d)+1, -1) AS LastDayOfYear,
        DATEADD(qq, DATEDIFF(qq, 0, @d), 0) AS FirstDayOfQuarter,
        DATEADD(qq, DATEDIFF(qq, 0, @d)+1, -1) AS LastDayOfQuarter,
        DATEADD(mm, DATEDIFF(mm, 0, @d), 0) AS FirstDayOfMonth,
        DATEADD(mm, DATEDIFF(mm, 0, @d)+1, -1) AS LastDayOfMonth,
        @d - DATEDIFF(dd, @@DATEFIRST - 1, @d) % 7 AS FirstDayOfWeek,
        @d - DATEDIFF(dd, @@DATEFIRST - 1, @d) % 7 + 6 AS LastDayOfWeek
    

    for just the day use day or datepart

     select DAY(getdate()),
         DATEPART(dd,GETDATE())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a function in SQL that we can use to validate First/Middle/Last names
I'm writing an interactive function that I'd like to have remember the last argument
I am writing a SQL Function that will take in a decimal and return
I am writing a scalar function in T-SQL. The function should insert leading zeros
Whats this syntax useful for : function(String... args) Is this same as writing function(String[]
I'm writing a function that fishes out the src from the first image tag
I'm writing a function for an installer DLL to verify the Authenticode signature of
Let's say that I'm writing a function to convert between temperature scales. I want
Say for instance I was writing a function that was designed to accept multiple
I am having some difficulty writing a function that will search through a directory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.