Greetings!
How do I add duration:
00:00:25
00:00:25
00:00:25
in a sql statement. I have been able to add just the
minutes or just the hours with the following statements
Here is the syntax to sum hours and minutes
Select sum(datepart(hour,convert(datetime,Duration))) from
reports.PhoneUsage /* sum hours*/
Select sum(datepart(minute,convert(datetime,Duration)))
from reports.PhoneUsage /* sum minutes */
but I would like to be able to produce a final result like
duration = 00:01:15 not just add one of the three hour,
minute, second
Thank you for your time and expertise!CREATE TABLE PhoneUsage (duration CHAR(8) PRIMARY KEY CHECK (duration LIKE
('[0-9][0-9]:[0-9][0-9]:[0-9][0-9]')))
INSERT INTO PhoneUsage VALUES ('00:00:25')
INSERT INTO PhoneUsage VALUES ('00:01:30')
SELECT SUM(DATEDIFF(SECOND,'19000101','1900-01-01T'+duration)) AS
total_seconds
FROM PhoneUsage
or:
SELECT
CONVERT(VARCHAR,DATEADD(SECOND,SUM(DATEDIFF(SECOND,'19000101','1900-01-01T'+
duration)),'19000101'),8) AS total_duration
FROM PhoneUsage
--
David Portas
--
Please reply only to the newsgroup
--|||Thank You for your help!
I really appreciate it. I am new to SQL Server and
still learning. I have gone through the steps you show
1. CREATE TABLE PhoneUsage (duration CHAR(8) PRIMARY KEY
CHECK (duration LIKE
>('[0-9][0-9]:[0-9][0-9]:[0-9][0-9]')))
>INSERT INTO PhoneUsage VALUES ('00:00:25')
>INSERT INTO PhoneUsage VALUES ('00:01:30')
2. SELECT SUM(DATEDIFF(SECOND,'19000101','1900-01-
01T'+duration)) AS
>total_seconds
> FROM PhoneUsage
or
SELECT
>CONVERT(VARCHAR,DATEADD(SECOND,SUM(DATEDIFF
(SECOND,'19000101','1900-01-01T'+
>duration)),'19000101'),8) AS total_duration
> FROM PhoneUsage
After creating the table, I now query and recieve a error
message telling me that I have a
Syntax error converting datetime from character string.
Would you might know what I have done wrong?
Thanks again for your help,
Mike
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment