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 8685765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:39:23+00:00 2026-06-12T22:39:23+00:00

Possible Duplicate: How to compare two dates to find time difference in SQL Server

  • 0

Possible Duplicate:
How to compare two dates to find time difference in SQL Server 2005, date manipulation

I had two datetime fields and should calculate the between them including time fields.

If the difference between them is less than 24 hours it should consider as 1 day and 24 to 48 hours as 2 days.

  • 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-06-12T22:39:25+00:00Added an answer on June 12, 2026 at 10:39 pm

    If you’re talking about SQL Server, DATEDIFF is what you need.

    Something like:

    SELECT DATEDIFF(d, StartDate, EndDate) FROM tableName
    

    … just substitute StartDate and EndDate for your column names, and tableName for the actual table name.
    Also, you can swap out the ‘d’ character for other dateparts, say if you wanted to find the date difference in months, years etc.

    * Update *

    @sateesh, try and follow this. I think the issue you may be having is to do with rounding in SQL’s DATEDIFF function. The way around this is to go down to a more granular level, such as minutes instead of days. See the sample code below, and compare the outputs:

    DECLARE @tblDummy TABLE(Value1 SMALLDATETIME, Value2 SMALLDATETIME, [Description] NVARCHAR(50))
    INSERT INTO @tblDummy (Value1, Value2, [Description]) VALUES ('2012-01-01 12:00', '2012-01-02 01:00', '13 hours 0 mins')
    INSERT INTO @tblDummy (Value1, Value2, [Description]) VALUES ('2012-01-01 12:00', '2012-01-02 11:59', '23 hours 59 mins')
    INSERT INTO @tblDummy (Value1, Value2, [Description]) VALUES ('2012-01-01 12:00', '2012-01-02 13:00', '25 hours 0 mins')
    INSERT INTO @tblDummy (Value1, Value2, [Description]) VALUES ('2012-01-01 12:00', '2012-01-03 12:00', '48 hours 0 mins')
    INSERT INTO @tblDummy (Value1, Value2, [Description]) VALUES ('2012-01-01 12:00', '2012-01-03 12:01', '48 hours 1 min')
    INSERT INTO @tblDummy (Value1, Value2, [Description]) VALUES ('2012-01-01 12:00', '2012-01-04 00:00', '60 hours 0 mins')
    
    -- Attempt 1: Standard date diff
    SELECT DATEDIFF(d, Value1, Value2) [diff], [Description]
    FROM @tblDummy
    
    -- Attempt 2: Date diff taking it down to the minutes level
    SELECT CEILING((DATEDIFF(minute, Value1, Value2) / 60.0) / 24.0) [diff], [Description]
    FROM @tblDummy
    

    Here’s the output:

    image displaying output from query

    I believe Attempt 2 gives you what you need. If that doesn’t help you, then I’m afraid I just don’t understand your question.

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

Sidebar

Related Questions

Possible Duplicate: Compare dates in java I am getting two dates from server in
Possible Duplicate: Value should be 0 between the two dates? SQL Server 2000 Table1
Possible Duplicate: How to compare two Dates without the time portion? How to compare
Possible Duplicate: MVC3 custom validation: compare two dates I am trying to make a
Possible Duplicate: How to find number of days between two dates using php I'm
Possible Duplicate: Days difference between two dates I need to know how many days
Possible Duplicate: Compare 2 dates with JavaScript Compare two dates in JavaScript I am
Possible Duplicate: How can I compare two dates, return a number of days I
Possible Duplicate: Algorithm to compare two images I am looking for solution to find
Possible Duplicate: php date compare PHP compare dates I have this code which permits

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.