Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare time in SQL Server?

I'm trying to compare time in a datetime field in a SQL query, but I don't know if it's right. I don't want to compare the date part, just the time part.

I'm doing this:

SELECT timeEvent  FROM tbEvents  WHERE convert(datetime, startHour, 8) >= convert(datetime, @startHour, 8) 

Is it correct?

I'm asking this because I need to know if 08:00:00 is less or greater than 07:30:00 and I don't want to compare the date, just the time part.

Thanks!

like image 486
André Miranda Avatar asked Apr 30 '09 16:04

André Miranda


People also ask

Can you compare datetime SQL?

The right way to compare date only values with a DateTime column is by using <= and > condition. This will ensure that you will get rows where date starts from midnight and ends before midnight e.g. dates starting with '00:00:00.000' and ends at "59:59:59.999".

Can we compare two timestamps in SQL?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR . To get the difference in seconds as we have done here, choose SECOND .

How do I find the difference between two times in SQL?

MySQL TIMEDIFF() Function The TIMEDIFF() function returns the difference between two time/datetime expressions. Note: time1 and time2 should be in the same format, and the calculation is time1 - time2.


1 Answers

Your compare will work, but it will be slow because the dates are converted to a string for each row. To efficiently compare two time parts, try:

declare @first datetime set @first = '2009-04-30 19:47:16.123' declare @second datetime set @second = '2009-04-10 19:47:16.123'  select (cast(@first as float) - floor(cast(@first as float))) -        (cast(@second as float) - floor(cast(@second as float)))        as Difference 

Long explanation: a date in SQL server is stored as a floating point number. The digits before the decimal point represent the date. The digits after the decimal point represent the time.

So here's an example date:

declare @mydate datetime set @mydate = '2009-04-30 19:47:16.123' 

Let's convert it to a float:

declare @myfloat float set @myfloat = cast(@mydate as float) select @myfloat -- Shows 39931,8244921682 

Now take the part after the comma character, i.e. the time:

set @myfloat = @myfloat - floor(@myfloat)  select @myfloat -- Shows 0,824492168212601 

Convert it back to a datetime:

declare @mytime datetime set @mytime = convert(datetime,@myfloat) select @mytime -- Shows 1900-01-01 19:47:16.123 

The 1900-01-01 is just the "zero" date; you can display the time part with convert, specifying for example format 108, which is just the time:

select convert(varchar(32),@mytime,108) -- Shows 19:47:16 

Conversions between datetime and float are pretty fast, because they're basically stored in the same way.

like image 72
Andomar Avatar answered Sep 21 '22 03:09

Andomar