Check if a time is between two times (time DataType)

前端 未结 7 1389
我寻月下人不归
我寻月下人不归 2020-12-06 10:05

I have a table that has a column \"Created\" as a datetime.

I\'m trying to query to check if the time for the Created value is between two times.

The Created

相关标签:
7条回答
  • 2020-12-06 10:09

    This should also work (even in SQL-Server 2005):

    SELECT *
    FROM dbo.MyTable
    WHERE Created >= DATEADD(hh,23,DATEADD(day, DATEDIFF(day, 0, Created - 1), 0))
      AND Created <  DATEADD(hh,7,DATEADD(day, DATEDIFF(day, 0, Created), 0))
    

    DEMO

    0 讨论(0)
  • 2020-12-06 10:11
    WITH CTE as
    (
    SELECT CAST(ShiftStart AS DATETIME) AS ShiftStart, 
    CASE WHEN ShiftStart > ShiftEnd THEN CAST(ShiftEnd AS DATETIME) +1
    ELSE CAST(ShiftEnd AS DATETIME) END AS ShiftEnd
    FROM **TABLE_NAME**
    )
    SELECT * FROM CTE
    WHERE 
    CAST('11:00:00' AS DATETIME) BETWEEN ShiftStart AND ShiftEnd -- Start of Shift
    OR CAST('23:00:00' AS DATETIME) BETWEEN ShiftStart AND ShiftEnd -- End of Shift
    
    0 讨论(0)
  • 2020-12-06 10:19

    I had a very similar problem and want to share my solution

    Given this table (all MySQL 5.6):

    create table DailySchedule
    (
      id         int auto_increment primary key,
      start_time time not null,
      stop_time  time not null
    );
    

    Select all rows where a given time x (hh:mm:ss) is between start and stop time. Including the next day.

    Note: replace NOW() with the any time x you like

    SELECT id
    FROM DailySchedule
    WHERE
      (start_time < stop_time AND NOW() BETWEEN start_time AND stop_time)
      OR
      (stop_time < start_time AND NOW() < start_time AND NOW() < stop_time)
      OR
      (stop_time < start_time AND NOW() > start_time)
    

    Results

    Given

      id: 1, start_time: 10:00:00, stop_time: 15:00:00
      id: 2, start_time: 22:00:00, stop_time: 12:00:00
    
    • Selected rows with NOW = 09:00:00: 2
    • Selected rows with NOW = 14:00:00: 1
    • Selected rows with NOW = 11:00:00: 1,2
    • Selected rows with NOW = 20:00:00: nothing
    0 讨论(0)
  • 2020-12-06 10:23

    Let us consider a table which stores the shift details

    enter image description here

    Please check the SQL queries to generate table and finding the schedule based on an input(time)

    Declaring the Table variable

    declare @MyShiftTable table(MyShift int,StartTime time,EndTime time)
    

    Adding values to Table variable

    insert into @MyShiftTable select 1,'01:17:40.3530000','02:17:40.3530000'
    insert into @MyShiftTable select 2,'09:17:40.3530000','03:17:40.3530000'
    insert into @MyShiftTable select 3,'10:17:40.3530000','18:17:40.3530000'
    

    Creating another table variable with an additional field named "Flag"

    declare @Temp table(MyShift int,StartTime time,EndTime time,Flag int)
    

    Adding values to temporary table with swapping the start and end time

    insert into @Temp select MyShift,case when (StartTime>EndTime) then EndTime else StartTime end,case when (StartTime>EndTime) then StartTime else EndTime end,case when (StartTime>EndTime) then 1 else 0 end from @MyShiftTable

    Creating input variable to find the Shift

    declare @time time=convert(time,'10:12:40.3530000')
    

    Query to find the shift corresponding to the time supplied

    select myShift from @Temp where
    (@time between StartTime and EndTime and Flag=0) or (@time not between StartTime and EndTime and Flag=1)

    0 讨论(0)
  • 2020-12-06 10:25

    I suspect you want to check that it's after 11pm or before 7am:

    select *
    from MyTable
    where CAST(Created as time) >= '23:00:00' 
       or CAST(Created as time) < '07:00:00'
    
    0 讨论(0)
  • 2020-12-06 10:27
    select *
    from MyTable
    where CAST(Created as time) not between '07:00' and '22:59:59 997'
    
    0 讨论(0)
提交回复
热议问题