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
 Sum of 2 time formatted column

Author  Topic 

nikoo56
Starting Member

26 Posts

Posted - 2012-02-29 : 12:44:00
I have 2 column in my query that return time format for example, The first column returns 3:20 and the next one return 2:40 , I like to have a new column that return the sum of these 2 column.


Column 1:

Select ltrim(xfer_1_elapsed/60) +':' + REPLACE(SPACE(CASE WHEN xfer_1_elapsed%60< 10 THEN 1 ELSE 0 END) + LTRIM(xfer_1_elapsed%60), ' ', '0') as xfer_1_elapsed,


column 2:

case when second_rep != orig_rep
then ltrim((xfer_2_elapsed - xfer_1_elapsed)/60) +':' + REPLACE(
SPACE(CASE WHEN (xfer_2_elapsed - xfer_1_elapsed)%60 < 10 THEN 1 ELSE 0 END), ' ', '0') + LTRIM((xfer_2_elapsed - xfer_1_elapsed)%60)
else null end as xfer_2_elapsed

what should I write for column 3 to do the sum? (please reply if you can write it)

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-29 : 12:51:21
can you post he table DDL along with sample data and expected results?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2012-02-29 : 22:55:49
[code]
select
*,
col3 = col1+col2
from
( -- Test Data
select col1 = convert(datetime,'3:20') ,col2 =convert(datetime,'2:40')
) a[/code]

Results:
[code]col1 col2 col3
----------------------- ----------------------- -----------------------
1900-01-01 03:20:00.000 1900-01-01 02:40:00.000 1900-01-01 06:00:00.000[/code]

CODO ERGO SUM
Go to Top of Page
   

- Advertisement -