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