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
 convert function for math operaton

Author  Topic 

alejo46
Posting Yak Master

157 Posts

Posted - 2010-03-27 : 16:27:46
I need your help
Ive got a table named TABLEX that contains 2 fields: started_date and ended_date, both are datetime type with the format YYYYMMDD HH:MM:SS.XXX.

In order to calculate the duration time, how can i substract two datetimes values and get the result in anothe columan?; in the example below, i use this with convert function.


select field1, started_date, ended_date, convert(varchar,ended_time,114) - convert(varchar,started_date,114) as "Duration Time" from TABLEX

but i've got an error basically saying that ocurred and error mistmath data types.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-03-27 : 17:20:04
See DATEDIFF function here
http://www.sqlteam.com/article/datediff-function-demystified


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-28 : 02:32:22
quote:
Originally posted by alejo46

I need your help
Ive got a table named TABLEX that contains 2 fields: started_date and ended_date, both are datetime type with the format YYYYMMDD HH:MM:SS.XXX.

In order to calculate the duration time, how can i substract two datetimes values and get the result in anothe columan?; in the example below, i use this with convert function.


select field1, started_date, ended_date, convert(varchar,ended_time,114) - convert(varchar,started_date,114) as "Duration Time" from TABLEX

but i've got an error basically saying that ocurred and error mistmath data types.


you want duration as integer value or in hh:mm:ss format?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-03-28 : 03:44:33
SELECT CONVERT(CHAR(8), Ended_Time - Started_Date, 8)
FROM TableX



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -