question

Mohan avatar image
Mohan asked

how to compare time in sqlserver2008

As per my requirement there are two columns in my db called from_time and to_time both are time data type. In the store procedure i am comparing both these timings. I am trying to compare both like following

while (from_time < to_time)

the above statement is not comparing...

Any help is highly appreciatable...

sql-server-2008t-sqldatetimedatatypes
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sp_lock avatar image
sp_lock answered

You could try this in your WHILE statement. Change the > 0 part to suit your needs.

  DATEDIFF(mi,date1,date2) > 0
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Mohan avatar image Mohan commented ·
my from_time is having a value "08:30" and to_time is having a value "03:00" now both these values i need to compare, whether the from_time is less than two_time or not.
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered

this compares fine on my SQL 2008 instance, so how are you actually doing the compare?

declare @from_time time
declare @to_time time

set @from_time = '08:30'
set @to_time = '03:00'


if @from_time < @to_time    
Print 'from time is earlier'

if @from_time > @to_time    
Print 'from time is later'
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.