Facebook Twitter Gplus LinkedIn YouTube Google Maps RSS
Home Technical Compare Time Part in SQL
formats

Compare Time Part in SQL

if a column in DB table is a datetime and you need to compare TIME only (without DATE) , there is no built in function for that.

Here is small Query to perform that task. The key is converting both to same Date
@time1 datetime
@time2 datetime
select @time1 = ’1900-01-01 07:30:00.000′,
@time2 = ’1899-12-30 07:30:00.000′
– This convert both @time1 & @time2 into same date (1900-01-01)
– Note : Resuls is a datetime
select tm1 = dateadd(day, datediff(day, 0, @time1) * -1, @time1),
– This uses convert function + substring to extract the time.
– Note : Result will be a varchar
tm2 = dateadd(day, datediff(day, 0, @time2) * -1, @time2)
select tm1 = substring(convert(varchar(25), @time1, 121), 12, 12),
tm2 = substring(convert(varchar(25), @time2, 121), 12, 12)

Compare T1 and T2

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

12,873 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© All rights reserved to Cyberbrutus. 2012
credit

Switch to our mobile site