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
 Math over datetime in varchar format??

Author  Topic 

phrankbooth
Posting Yak Master

162 Posts

Posted - 2008-06-17 : 19:51:10
Hello,

I have this column of time values:
00:00:03.3592675
00:00:00
00:00:03.8592515
00:00:03.6873820
00:00:03.8436270
00:00:03.3436430

It is in varchar format. I tried converting it to datetime but get an invalid format error message.

How can I sum up these values and average them out?

Thanks!

--PhB

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-17 : 19:57:30
You can't sum or avg datetime data:

quote:

Operand data type datetime is invalid for sum operator.



Here's one way to convert to datetime:

DECLARE @t table (TimeColumn varchar(16))

INSERT INTO @t VALUES ('00:00:03.3592675')
INSERT INTO @t VALUES ('00:00:00')
INSERT INTO @t VALUES ('00:00:03.8592515')
INSERT INTO @t VALUES ('00:00:03.6873820')
INSERT INTO @t VALUES ('00:00:03.8436270')
INSERT INTO @t VALUES ('00:00:03.3436430')

SELECT CONVERT(datetime, '01/01/1900 ' + SUBSTRING(TimeColumn, 1, 12))
FROM @t


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -