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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:33:45+00:00 2026-05-21T03:33:45+00:00

I have a DateTime column. I want to extract all records, lets say, from

  • 0

I have a DateTime column. I want to extract all records, lets say, from 8:30 to 16:15 within a certain date range. My problem is that I need to compare hour and minute as a single time value. I can test the DATEPART for Greater or Less than some hours value, but if I then do that for minutes my query will fail if the later-in-the-day time has a smaller minutes value.

I have looked at INTERVAL, BETWEEN, DATEPART, DATEDIFF etc, but don’t see quite how to to this without a “TimeOfDay” value that I can use across records of different dates.

I have tried subtracting the year, month and day parts of the date so that I can compare just the time of day, but when attmpting to subract, say, the year part of a date I get an overlfow error:

This part works:

select - cast( DATEPART(YEAR, CallTime) as integer) from history

This fails:

select DATEADD(YEAR, - cast( DATEPART(YEAR, CallTime) as integer), CallTime) 
from history where calltime is not null

I have also tried casting the hours and minutes parts to chars, concatenating them and comparing to my target range, but this also fails.

I believe newer versions of SQL server may have a function to deal with this situation, but that’s not available to me.

I hope and imagine there is a simple, obvious solution to this, but it’s eluding me.

  • 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-21T03:33:45+00:00Added an answer on May 21, 2026 at 3:33 am

    Try creating a “MinuteOfDay” function that calculates how many minutes have passed in the day based on a datetime.

    CREATE FUNCTION dbo.[MinuteOfDay] 
    (
        @dt datetime
    )
    RETURNS int
    AS
    BEGIN
        RETURN (datepart(hh,@dt)*60) + datepart(mi,@dt) 
    END
    

    then use the result of that function to filter.

    select * 
    from MyTable t 
    where dbo.MinuteOfDay(t.SomeDateTimeColumn) between dbo.MinuteOfDay('1900-1-1 08:30:00') and dbo.MinuteOfDay('1900-1-1 16:15:00')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DateTime which I want to store in a Date MySQL column.
I have a column which has a datatype : datetime. But now i want
In SQL Server I have a DATETIME column which includes a time element. Example:
I have a nullable DateTime column in my SQL Server 2005 table called DateTimeDeleted.
I have a sqlite (v3) table with this column definition: timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
Having a table with a column like: mydate DATETIME ... I have a query
I have two DateTime objects: StartDate and EndDate . I want to make sure
I have a datetime coming back from an XML file in the format: 20080916
If I have a table that (among other columns) has two DATETIME columns, how
I have a DateTime object that I need to print in a custom gridlike

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.