Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Find No of hours bewteen two times

Author  Topic 

manish26
Starting Member

3 Posts

Posted - 2015-04-02 : 15:39:33
I need to write a SQL to find number of hours between a begin time and end time. The fields are varchar. There are several date functions in sql, but I am not able to figure out how to get the hours between two times that is not in date format. Any help is appreciated. Thanks!

BEGIN_TIME END_TIME
0900 ----- 1500
1000 ----- 1700
1000 ----- 1230
0930 ----- 1030

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-04-02 : 16:04:24
select datediff(hour, BEGIN_TIME, END_TIME)
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2015-04-02 : 16:17:44
You'll need to fully define what you mean by "number of hours". That sounds odd, and beyond pedantic, but let me put a few examples out for you to consider:
090000 -- 1000 - Clearly an hour difference
095959 -- 100000 - Is this an hour's difference? In real time, it's one second.
090000 -- 105959 - In real time its almost two hours. How do you want to recognize the difference?



I would rather be the man who bought the Brooklyn Bridge than the one who sold it. -Will Rogers
Go to Top of Page

manish26
Starting Member

3 Posts

Posted - 2015-04-02 : 16:47:37
Hi Bustaz,
These are basically the class start and end time. It's only in hours and mintues no secs invloces

0900 - 1015 would be 1.16 hrs
0900 - 1059 would be 1.98 hrs

Thanks!
Go to Top of Page

manish26
Starting Member

3 Posts

Posted - 2015-04-02 : 16:51:30
Hi gbritton,
I am in oracle database and datediff doesn't work in oracle.

Thanks
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-04-02 : 22:25:52
ORacle? This is a SQL Server forum. You might want to try an Oracle forum
Go to Top of Page
   

- Advertisement -