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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:14:36+00:00 2026-06-10T16:14:36+00:00

I have table i.e. BookingDetails . BookingDetails containt following fields. CustomerID DateFrom DateTo TimeFrom

  • 0

I have table i.e. BookingDetails. BookingDetails containt following fields.

CustomerID

DateFrom

DateTo

TimeFrom

TimeTo

BookingDetails contains n records.

CustomerID    DateFrom     DateTo       TimeFrom    TimeTo
11137         2012-08-14   2012-08-16   00:33:46    03:33:46
11138         2012-08-15   2012-08-17   08:00:00    00:31:03
11139         2012-08-16   2012-08-17   22:46:25    00:46:25

I want to select records between given information DateFrom, DateTo, TimeFrom, TimeTo.

I have do following query

declare @fDate date
set @fDate = '2012-08-14' 

declare @tDate date
set @tDate = '2012-08-16'

declare @fTime time
set @fTime ='12:33:46 AM'

declare @tTime time
set @tTime='12:31:03 AM'

SELECT BookingDetails.CustomerID
FROM BookingDetails 
WHERE  (DateFrom between @fDate and @tDate) And (BookingDetails.DateFrom >= @fDate     and BookingDetails.DateTo<=@tDate)
and(TimeFrom between CONVERT(varchar(15),cast(@fTime as time) , 108) and CONVERT(varchar(15),cast(@tTime as time) , 108))
and (TimeFrom >=CONVERT(varchar(15),cast(@fTime as time) , 108) and TimeTo <=CONVERT(varchar(15),cast(@tTime as time) , 108)) 

Time save in database is in 24 hours format. Time used in query is 12Hours format thats why i convert it to 24 hours format in query.

Is this query is correct or I have to change it?

This Query doesn’t return any value. I want to select records between @fDate , @tDate, @fTime, @tTime

I expect Result for first two customerID i.e. 11137,11138

  • 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-10T16:14:38+00:00Added an answer on June 10, 2026 at 4:14 pm

    Why not pass the start and end as DATETIME instead of as separate values? In fact, why are you storing DATE and TIME separately when it’s clear these are points in time and the two values are more important together than apart? Anyway given the current schema you need to stop converting to string. Try this:

    DECLARE @b TABLE (  
       CustomerID INT,
       DateFrom DATE,
       DateTo DATE,
       TimeFrom TIME,
       TimeTo TIME 
    );
    
    INSERT @b VALUES (11137,'2012-08-14','2012-08-16','00:33:46','03:33:46'),
                     (11138,'2012-08-15','2012-08-17','08:00:00','00:31:03'),
                     (11139,'2012-08-16','2012-08-17','22:46:25','00:46:25');
    
    declare @fDate date
    set @fDate = '2012-08-14' 
    
    declare @tDate date
    set @tDate = '2012-08-16'
    
    declare @fTime time
    set @fTime ='12:33:46 AM'
    
    declare @tTime time
    set @tTime='12:31:03 AM'
    
    ;WITH x AS 
    (
      SELECT 
        CustomerID, DateFrom, TimeFrom, DateTo, TimeTo,
        [Start] = DATEADD(SECOND, DATEDIFF(SECOND,'00:00',TimeFrom), 
                  CONVERT(DATETIME, DateFrom)),
        [End]   = DATEADD(SECOND, DATEDIFF(SECOND,'00:00',TimeTo),   
                  CONVERT(DATETIME, DateTo))
      FROM @b
    )
    SELECT * FROM x WHERE [Start] 
      BETWEEN     CONVERT(DATETIME, @fDate) + CONVERT(DATETIME, @fTime)
              AND CONVERT(DATETIME, @tDate) + CONVERT(DATETIME, @tTime);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table with 300 000 records (MyISAM). I get record from this table
I have table table1 **************************** id | start_date | end_date 1 30/06/2012 01/01/9999 When
I have table A with fields user1 and user2 and table B with user3
I have table with statistical data. I'm trying to count the records grouped by
I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have table the following data structure in SQL Server: ID Date Allocation 1,
I have table of products, and there are 2 fields: price, currency. Possible values
I have table like below ID User Date Location 1 Tom 6-Mar-2012 US 2
I have table that contains more than 12 millions of rows. I need to
i have table (MEN) with 900,000 records. in this table i have field IP

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.