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. Comparing only Time in Sql?

Comparing only Time in Sql?

Scheduled Pinned Locked Moved Database
databasebusinesstutorialquestion
2 Posts 2 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.
  • M Offline
    M Offline
    majidbhutta
    wrote on last edited by
    #1

    Comparing only Time in Sql? Hi Dears! I have two Different DateTime Values i want to Compare only their times independent of Dates How to do it? let DateTime @dt1='2004-10-30 19:30:00.917' DateTime @dt2='2005-11-29 15:00:00.917' accordint to my requirements @dt1>=@dt2 should be true as 19:30>=15:00 because i have to compare only the time not the Date how to achieve this in sql .Function required.

    M 1 Reply Last reply
    0
    • M majidbhutta

      Comparing only Time in Sql? Hi Dears! I have two Different DateTime Values i want to Compare only their times independent of Dates How to do it? let DateTime @dt1='2004-10-30 19:30:00.917' DateTime @dt2='2005-11-29 15:00:00.917' accordint to my requirements @dt1>=@dt2 should be true as 19:30>=15:00 because i have to compare only the time not the Date how to achieve this in sql .Function required.

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      You can change your times to milliseconds since midnight and compare those.

      CREATE FUNCTION MillisecondsSinceMidnight
      (
          @in DATETIME
      )
      RETURNS INTEGER
      AS
      BEGIN
          DECLARE @retval INTEGER
      	
          SET @retval =
                  DATEPART(ms,@in) +
                  DATEPART(ss,@in) * 1000 +
                  DATEPART(mi,@in) * 60000 +
                  DATEPART(hh,@in) * 3600000
      
          RETURN @retval
      END
      
      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