how to get rows from two datetime fields with hours on morning time.

  • I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

  • marcelo salgado - Monday, November 5, 2018 6:16 PM

     I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    Kindly post DDL with sample data and expected result.

    Saravanan

  • marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

  • Jonathan AC Roberts - Tuesday, November 6, 2018 6:33 AM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

    Jonathan....Many, many thanks !!!!

  • saravanatn - Monday, November 5, 2018 7:34 PM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

     I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    Kindly post DDL with sample data and expected result.

    Saravanatn ...many thanks for your time....
    I could solve it !!

  • marcelo salgado - Tuesday, November 6, 2018 6:56 AM

    Jonathan AC Roberts - Tuesday, November 6, 2018 6:33 AM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

    Jonathan....Many, many thanks !!!!

    Since you posted this in a SQL Server 2017 forum, you could also do this:

    select *
    from [Crimes] as [c]
    where [c].[CrimeDate]
      between '20180301' and '20180901'
      and cast([c].[CrimeDate] as time) >= cast('23:00' as time)
      or cast([c].[CrimeDate] as time) <= cast('06:00' as time);

  • Jonathan AC Roberts - Tuesday, November 6, 2018 6:33 AM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

    Converting date/time data to strings is horribly expensive.  You're better off using DATE/TIME functions for this.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND CAST(DATEADD(HOUR, 1, c.CrimeDate) AS TIME) <= '07:00'

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Lynn Pettis - Tuesday, November 6, 2018 8:42 AM

    marcelo salgado - Tuesday, November 6, 2018 6:56 AM

    Jonathan AC Roberts - Tuesday, November 6, 2018 6:33 AM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

    Jonathan....Many, many thanks !!!!

    Since you posted this in a SQL Server 2017 forum, you could also do this:

    select *
    from [Crimes] as [c]
    where [c].[CrimeDate]
      between '20180301' and '20180901'
      and cast([c].[CrimeDate] as time) >= cast('23:00' as time)
      or cast([c].[CrimeDate] as time) <= cast('06:00' as time);
    [/cod

    Lynn Pettis - Tuesday, November 6, 2018 8:42 AM

    marcelo salgado - Tuesday, November 6, 2018 6:56 AM

    Jonathan AC Roberts - Tuesday, November 6, 2018 6:33 AM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

    [/quote-4]

    Jonathan....Many, many thanks !!!!

    [/quote-5]

    Since you posted this in a SQL Server 2017 forum, you could also do this:

    select *
    from [Crimes] as [c]
    where [c].[CrimeDate]
      between '20180301' and '20180901'
      and cast([c].[CrimeDate] as time) >= cast('23:00' as time)
      or cast([c].[CrimeDate] as time) <= cast('06:00' as time);

    Great Lynn!!! Thanks a lot!

    [/quote-6]

  • Lynn Pettis - Tuesday, November 6, 2018 8:42 AM

    marcelo salgado - Tuesday, November 6, 2018 6:56 AM

    Jonathan AC Roberts - Tuesday, November 6, 2018 6:33 AM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

    Jonathan....Many, many thanks !!!!

    Since you posted this in a SQL Server 2017 forum, you could also do this:

    select *
    from [Crimes] as [c]
    where [c].[CrimeDate]
      between '20180301' and '20180901'
      and cast([c].[CrimeDate] as time) >= cast('23:00' as time)
      or cast([c].[CrimeDate] as time) <= cast('06:00' as time);

    You need brackets around the two items OR'd together.

  • Jonathan AC Roberts - Tuesday, November 6, 2018 6:33 AM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

    Don't use this method, performs horribly!
    😎

    Drew's method is the best one posted so far, shifting the time to simplify the search condition is much more efficient than dual conditions.

  • Eirikur Eiriksson - Wednesday, November 7, 2018 12:32 AM

    Jonathan AC Roberts - Tuesday, November 6, 2018 6:33 AM

    marcelo salgado - Monday, November 5, 2018 6:16 PM

    I have this problem How can I get rows in this example.
    We are looking for crimes in our database on my Crimes table, where crimes were committed from March 2018 to August 2018 from 23:00 p.m. to 06:00 a.m.
    of the next day. in that table we have a datetime field where the date and time are saved.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND (SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) >= '23:00'
             OR SUBSTRING(CONVERT(varchar,c.CrimeDate,121),12,5) <= '06:00')

    Don't use this method, performs horribly!
    😎

    Drew's method is the best one posted so far, shifting the time to simplify the search condition is much more efficient than dual conditions.

    Thank's guys for the effort to teach me

  • drew.allen - Tuesday, November 6, 2018 9:45 AM

    Converting date/time data to strings is horribly expensive.  You're better off using DATE/TIME functions for this.

    SELECT *
    FROM Crimes c
    WHERE c.CrimeDate BETWEEN '20180301' AND '20180901'
       AND CAST(DATEADD(HOUR, 1, c.CrimeDate) AS TIME) <= '07:00'

    Drew

    I'm going to put this one in my back pocket for another day.  It's not intuiative but it makes so much sense.  Thanks for providing that example Drew.

    Cheers,


    SELECT quote FROM brain WHERE original = 1
    0 rows returned

  • If this is something that needs to be accomplished on a regular basis, add a persisted computed column to the table with the TIME datatype  so there's a better chance of being able to use an index.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply