Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Working with just time section of a datetime type

Working with just time section of a datetime type

Scheduled Pinned Locked Moved Database
questiondatabase
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Den2Fly
    wrote on last edited by
    #1

    Hello again buddies, I wonder how can I work with just time section of a datetime or smalldatetime object ignoring its date section. wouldn't it be better to provide a "time" data type? What I am looking for is to query records that a datetime column of them is in a specified time with no importance on the date --- "Art happens when you least expect it."

    C G 2 Replies Last reply
    0
    • D Den2Fly

      Hello again buddies, I wonder how can I work with just time section of a datetime or smalldatetime object ignoring its date section. wouldn't it be better to provide a "time" data type? What I am looking for is to query records that a datetime column of them is in a specified time with no importance on the date --- "Art happens when you least expect it."

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Use DATEPART(). It is a bit of a mess if you want to do something like get me the records between 11:30 and 14:30 but it is doable.


      Do you want to know more?

      D 1 Reply Last reply
      0
      • D Den2Fly

        Hello again buddies, I wonder how can I work with just time section of a datetime or smalldatetime object ignoring its date section. wouldn't it be better to provide a "time" data type? What I am looking for is to query records that a datetime column of them is in a specified time with no importance on the date --- "Art happens when you least expect it."

        G Offline
        G Offline
        Grimolfr
        wrote on last edited by
        #3

        Set them all to the same date. CAST('1900-01-01T' + SUBSTRING(CONVERT(varchar, theTime, 126), 12, 12) AS datetime)


        Grim

        (aka Toby)

        MCDBA, MCSD, MCP+SB

        Need a Second Life?[^]

        SELECT * FROM user WHERE clue IS NOT NULL GO

        (0 row(s) affected)

        J D 2 Replies Last reply
        0
        • G Grimolfr

          Set them all to the same date. CAST('1900-01-01T' + SUBSTRING(CONVERT(varchar, theTime, 126), 12, 12) AS datetime)


          Grim

          (aka Toby)

          MCDBA, MCSD, MCP+SB

          Need a Second Life?[^]

          SELECT * FROM user WHERE clue IS NOT NULL GO

          (0 row(s) affected)

          J Offline
          J Offline
          Jon Rista
          wrote on last edited by
          #4

          Sorry, this is off topic, but: "SELECT * FROM user WHERE clue IS NOT NULL GO (0 row(s) affected)" <--- :laugh:

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            Use DATEPART(). It is a bit of a mess if you want to do something like get me the records between 11:30 and 14:30 but it is doable.


            Do you want to know more?

            D Offline
            D Offline
            Den2Fly
            wrote on last edited by
            #5

            Yes it works, thank you so much Colin :-) I am sorry if my questions seems so amature, I guess I am a good programmer but a newbie in SQL which I feel is a great different programming environment. I promiss that will change!!!! ;-) --- "Art happens when you least expect it."

            C 1 Reply Last reply
            0
            • G Grimolfr

              Set them all to the same date. CAST('1900-01-01T' + SUBSTRING(CONVERT(varchar, theTime, 126), 12, 12) AS datetime)


              Grim

              (aka Toby)

              MCDBA, MCSD, MCP+SB

              Need a Second Life?[^]

              SELECT * FROM user WHERE clue IS NOT NULL GO

              (0 row(s) affected)

              D Offline
              D Offline
              Den2Fly
              wrote on last edited by
              #6

              Seems a great trick, what about the performance? DATEPART or CAST?? --- "Art happens when you least expect it."

              G 1 Reply Last reply
              0
              • D Den2Fly

                Yes it works, thank you so much Colin :-) I am sorry if my questions seems so amature, I guess I am a good programmer but a newbie in SQL which I feel is a great different programming environment. I promiss that will change!!!! ;-) --- "Art happens when you least expect it."

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Den2Fly wrote: a newbie in SQL .... I promiss that will change!!!! We've all got to start somewhere.


                Do you want to know more?

                1 Reply Last reply
                0
                • D Den2Fly

                  Seems a great trick, what about the performance? DATEPART or CAST?? --- "Art happens when you least expect it."

                  G Offline
                  G Offline
                  Grimolfr
                  wrote on last edited by
                  #8

                  As Colin indicated in his original response, usability and efficiency both depend greatly on exactly what you're doing with it. Also, I've found that when you take two solutions to the same problem and compare them, it's possible that one solution is faster on one server, and the other is faster on a different one. For instance, stripping the time from a datetime. There are many ways to do this, the two most common being CONVERT(varchar(8), theDate, 112) or DATEADD(day, 0, DATEDIFF(day, 0, theDate)). I find that DATEADD/DATEDIFF runs faster in our development environment by a factor of 5, but the CONVERT method is faster in our production environment by a factor of 2. So, the short answer is that you should write the query both ways, and benchmark them on your own system to see which one is faster on your own server. It also depends greatly on where/how you're using it. If you're using it in the WHERE clause, either one of them is going to be a pig. If you're just using it in the SELECT clause, then it will probably depend on your system hardware and configuration.


                  Grim

                  (aka Toby)

                  MCDBA, MCSD, MCP+SB

                  Need a Second Life?[^]

                  SELECT * FROM user WHERE clue IS NOT NULL GO

                  (0 row(s) affected)

                  D 1 Reply Last reply
                  0
                  • G Grimolfr

                    As Colin indicated in his original response, usability and efficiency both depend greatly on exactly what you're doing with it. Also, I've found that when you take two solutions to the same problem and compare them, it's possible that one solution is faster on one server, and the other is faster on a different one. For instance, stripping the time from a datetime. There are many ways to do this, the two most common being CONVERT(varchar(8), theDate, 112) or DATEADD(day, 0, DATEDIFF(day, 0, theDate)). I find that DATEADD/DATEDIFF runs faster in our development environment by a factor of 5, but the CONVERT method is faster in our production environment by a factor of 2. So, the short answer is that you should write the query both ways, and benchmark them on your own system to see which one is faster on your own server. It also depends greatly on where/how you're using it. If you're using it in the WHERE clause, either one of them is going to be a pig. If you're just using it in the SELECT clause, then it will probably depend on your system hardware and configuration.


                    Grim

                    (aka Toby)

                    MCDBA, MCSD, MCP+SB

                    Need a Second Life?[^]

                    SELECT * FROM user WHERE clue IS NOT NULL GO

                    (0 row(s) affected)

                    D Offline
                    D Offline
                    Den2Fly
                    wrote on last edited by
                    #9

                    You are right. Thank you. --- "Art happens when you least expect it."

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups