Author |
Topic |
hisouka
Starting Member
28 Posts |
Posted - 2007-06-25 : 04:02:16
|
How can i compute Total Hours? of these table? TotalHours must be twodecimal places for ex(10.23)EmpNo......Date......TimeIn.....TimeOut.......TotalHours123......1/1/2007.....7:00.......14:00............?124......1/1/2007.....8:33.......15:34............?123......1/2/2007.....10:30......20:58............?124......1/2/2007.....7:06.......15:36............? |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-06-25 : 04:12:17
|
What is the data type for the TimeIn and TimeOut columns?Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
hisouka
Starting Member
28 Posts |
Posted - 2007-06-25 : 04:23:15
|
Sir, only varchar(50)tnx |
 |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-25 : 04:40:10
|
SELECT EmpNo, Date, TimeIn, TimeOut, CONVERT(numeric(4, 2), CONVERT(numeric(4, 2), LEFT(TimeOut, CHARINDEX(':', TimeOut)- 1)) + CONVERT(numeric(4, 2), RIGHT(TimeOut, LEN(TimeOut) - CHARINDEX(':', TimeOut))) / 60 - CONVERT(numeric(4, 2), LEFT(TimeIn, CHARINDEX(':', TimeIn)- 1)) - CONVERT(numeric(4, 2), RIGHT(TimeIn, LEN(TimeIN) - CHARINDEX(':', TimeIn))) / 60) FROM YourTable |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-25 : 04:51:14
|
What is the datatype od date column?If you used datetime column for both timein and timeout, it is just using datediff function over themDatediff(hour,timein,timeout)MadhivananFailing to plan is Planning to fail |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-06-25 : 04:59:05
|
[code]-- prepare sample datadeclare @t table( EmpNo int, Date datetime, TimeIn varchar(50), TimeOut varchar(50))insert @tselect 123,'1/1/2007','7:00','14:00' union allselect 124,'1/1/2007','8:33','15:34' union allselect 123,'1/2/2007','10:30','20:58' union allselect 124,'1/2/2007','7:06','15:36'-- desired queryselect EmpNo, [Date], TimeIn, TimeOut, replace(convert(varchar(5), cast(TimeOut as datetime) - Cast(TimeIn as datetime), 108), ':', '.') as TotalHoursFrom @t[/code]Note: This is purely based on the sample data you posted. It will not work if time difference spans across dates.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
hisouka
Starting Member
28 Posts |
Posted - 2007-06-25 : 05:13:28
|
Hi harsh_athalye,Thanks. it works.. But how can i conver 09.42 result to 9.42 only? based on ur SP.. thanks |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-06-25 : 05:18:49
|
Just convert it to Numeric(25,2):-- prepare sample datadeclare @t table( EmpNo int, Date datetime, TimeIn varchar(50), TimeOut varchar(50))insert @tselect 123,'1/1/2007','7:00','14:00' union allselect 124,'1/1/2007','8:33','15:34' union allselect 123,'1/2/2007','10:30','20:58' union allselect 124,'1/2/2007','7:06','15:36'-- desired queryselect EmpNo, [Date], TimeIn, TimeOut, convert(numeric(25,2), replace(convert(varchar(5), cast(TimeOut as datetime) - Cast(TimeIn as datetime), 108), ':', '.')) as TotalHoursFrom @t Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
hisouka
Starting Member
28 Posts |
Posted - 2007-06-25 : 05:48:37
|
Thanks very much.. |
 |
|
|
|
|