Iam having a table with column name TotalHrs. Its data type is varchar
Time format is HH.MM.SS
I want to sum the column TotalHrs.
TotalHrs Date
00:56:30 AM 5/6/2012
02:08:40 AM 5/6/2012
01:01:00 AM 5/6/2012
and get the o/p like this.
O/P ==> 04:05:10.0000000
How can i get the above result
The query that I had wrote
SELECT SUM (HoursWorked) Total FROM logout where Date='5/6/2012'
I got the error message like this.....
Operand data type varchar is invalid for sum operator....
I need the query
Loading


Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted May 7, 2012, 4:46 AM EDT
select convert(char(8),dateadd(second,SUM ( DATEPART(hh,(convert(datetime,HoursWorked,1))) * 3600 + DATEPART(mi, (convert(datetime,HoursWorked,1))) * 60 + DATEPART(ss, (convert(datetime,HoursWorked,1))) ),0),108)
FROM logout where Date='5/6/2012'
SenthilkumarRank #387 · 6.5K reputationPosted May 6, 2012, 8:11 PM EDT
You need to do the type conversion. What it says the varchar type can't be sum. You can do the some of the int, float, double but not the varchar type.
Either you need to use the CAST or CONVERT.
You can refer this article.
http://msdn.microsoft.com/en-us/library/ms187928.aspx
selvi subramanianRank #2,007 · 723 reputationPosted May 6, 2012, 8:52 PM EDT
The above query gives the result like this.......
Total 0
selvi subramanianRank #2,007 · 723 reputationPosted May 8, 2012, 11:28 PM EDT