Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have a table like below


Id int

Value nvarchar

Values in the table are like below

ID Value
1 10
2 20
3 30
4 9:15
5 10:15
6 8:10
7 9:15
8 40
9 50

In above table Value contains both int and time .
Now i need the sql query to sum all the in t values and Time values

Please help me to achive this ..

Thanks
Mohan
Posted
Comments
Darshan.Pa 7-May-14 1:15am    
what kind of output you want, in seconds or in time or any other format?
V Mohan 7-May-14 1:22am    
I want two outputs one is sum of all integer values as integer and another one is sum of all time in time format.

Thanks
Darshan.Pa 7-May-14 1:48am    
do you have any other column that will identify your column value is interger or Time.

Otherwise in where condition check for int or time
V Mohan 7-May-14 1:50am    
I tried, But it shows error. Please help me to get sum of all int and sum of all time value in a query
Darshan.Pa 7-May-14 2:01am    
try now i have posted solution

Try this

to add integer value :
SQL
select sum(cast(value as float))From ##table where not value like '%:%'


To add Time value
SQL
select convert(time,dateadd(s,SUM(
( DATEPART(hh, value) * 3600 ) +
( DATEPART(mi, value) * 60 ) +
 DATEPART(ss, value)),0)) From ##table where value like '%:%'
 
Share this answer
 
v3
Comments
V Mohan 7-May-14 2:19am    
Thanks for yor reply. Second query is working fine. But the first query is showing error as follows

Conversion failed when converting the nvarchar value '4.3' to data type int.
Maciej Los 7-May-14 2:22am    
First query uses NOT LIKE. Did you wrote it properly?
V Mohan 7-May-14 2:19am    
Because value is nvarchar type
King Fisher 7-May-14 2:23am    
make it as float
Maciej Los 7-May-14 2:20am    
+5!
Another solution is:
SQL
DECLARE @tmp TABLE (ID INT IDENTITY(1,1), Value VARCHAR(10))

INSERT INTO @tmp (Value)
VALUES('10'),('20'),('30'),('9:15'),('10:15'),
        ('8:10'),('9:15'),('40'),('50')


SELECT DATEADD(ms, SUM(DATEDIFF(ms, '00:00:00.000', T1.TValue )), '00:00:00.000') AS SumOfTime
FROM (
    SELECT ID, Value AS TValue
    FROM @tmp
    WHERE CHARINDEX(':', Value)>0
    ) AS T1


SELECT SUM(CONVERT(INT,T2.IValue)) AS SumOfInt
FROM (
    SELECT ID, Value AS IValue
    FROM @tmp
    WHERE CHARINDEX(':', Value)=0
    ) AS T2


Results:
SumOfTime
1900-01-02 12:55:00.000  - it means 2 days and 12 hrs 55 mins

SumOfInt
150
 
Share this answer
 
Comments
King Fisher 7-May-14 2:28am    
no words :)
Maciej Los 7-May-14 2:29am    
Thank you ;)
Tom Marvolo Riddle 7-May-14 2:42am    
Sure +5!
Maciej Los 7-May-14 2:53am    
Thank you ;)
you can do it like this

this will give you sum of all integers

SQL
SELECT Sum(value)
    FROM YourTable
    WHERE ISNUMERIC(value + '.0e0') = 1


this will give you sum of all time
SELECT Sum(convert(time,value))
    FROM YourTable
     WHERE ISNUMERIC(value + '.0e0') != 1
 
Share this answer
 
v3
Comments
Maciej Los 7-May-14 1:58am    
I'm sure you'll get error message: Msg 8117, Operand data type time is invalid for sum operator. for the second query.
Darshan.Pa 7-May-14 2:01am    
try now answer is updated
V Mohan 7-May-14 2:11am    
Because the Data type for Value is nvarchar. So its shows error as below

Operand data type time is invalid for sum operator.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900