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

  • SEARCH
  • Home
  • 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 6898045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:14:47+00:00 2026-05-27T07:14:47+00:00

I thought it was clear, but doesn’t seem so. This question is about T-SQL

  • 0

I thought it was clear, but doesn’t seem so.
This question is about T-SQL (since it’s tagged with tsql 🙂 )

So I couldn’t find any out-of-the-box solution to calculate my problem.

Let’s assume you have these two dateTimes:

DECLARE @start DATETIME = '2011-01-01',
    @end DATETIME = '2011-04-15'

The difference of these two datetimes in Days should be quivalent to 105.

The calculation works as follows: For every full month add 30 days, for the rest add the days till the date is achieved.

I could program this, but it would be an enormous SQL-statement, which I find find kinda ugly.

Is there any simple solution for this, like a built-in function or something short?

Thanks in advance.

  • 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-27T07:14:48+00:00Added an answer on May 27, 2026 at 7:14 am

    Short Answer

    There’s no built in function, but you could pretty easily create your own to handle converting a datetime to an int. From there, the SQL you would have to write would be trivial.

    Long Answer

    There’s no built in function that will do this, probably because every month doesn’t have 30 days. 🙂

    You can start with this:

    DECLARE @start DATETIME = '2011-01-01',
            @end DATETIME = '2011-04-15'
    
    DECLARE @endConverted INT
    SELECT @endConverted = DATEPART(month, @end) * 30 
                           + CASE 
                               WHEN DATEPART(DAY, @end) <= 30 
                                 THEN datepart(DAY, @end)  
                               ELSE 30 
                             END
    
    DECLARE @startConverted INT
    SELECT @startConverted = DATEPART(MONTH, @start) * 30
                             + CASE
                                 WHEN DATEPART(DAY, @start) <= 30
                                   THEN DATEPART(DAY, @start)
                                 ELSE 30
                               END
    
    SELECT @endConverted - @startConverted
    

    This isn’t beautiful SQL, but it works. Note that it returns 104 (because 15 days – 1 day = 14 days), but simple enough to tack on a + 1 to the end of the final select if you want to handle the boundry days differently.

    Note that the math here could pretty easily be moved into a function, which would allow you to clean your SQL up. Let’s assume you created a function called GetDateTimeAsInt which holds the math; your SQL could be as simple as

    DECLARE @start INT = GetDateTimeAsInt('2011-01-01'),
            @end INT = GetDateTimeAsInt('2011-04-15')
    
    SELECT @end - @start -- may need to add 1 here
    

    In my testing, this seems to work. It will return the same result as the DATEDIFF function for the date range you specify in your post, but this is because there are 2 days with 31 days and 1 day with 28, so effectively, Jan – April have 30 days each. If you use it with a wider date range, you’ll begin to get different results with my code vs. the DATEDIFF function.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope the title of this question is somewhat clear, I couldn't figure out
I've posted this here , but thought it might deserve a question on its
I thought I heard that py2exe was able to do this, but I never
I thought I had seen a bug report about this on the jQuery site,
EDIT: This question is solved, but I can't accept my own answer just yet.
I was reading some posts about closures and saw this everywhere, but there is
This question seems clear what is happening: I already have a connection open, the
I know this question sounds weird, but please, let me explain myself. I'm using
I have looked into this, but I’m still not quite clear how to do
What's a better way to clear cin input? I thought cin.clear and cin.ignore was

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.