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 JayaramanPosted May 7, 2012, 8:46 AM
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'
selvi subramanianPosted May 9, 2012, 3:28 AM
selvi subramanianPosted May 7, 2012, 12:52 AM
The above query gives the result like this.......
Total 0
SenthilkumarPosted May 7, 2012, 12:11 AM
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