how to just get time from a date

Chris Jones
Chris Jones used Ask the Experts™
on
hello

I am just trying to get a time from the query I have and its giving me the default date then my time. The time is the only part that I need in that field.

What i get
1900-01-01 00:00:55.000

Open in new window


what i need
00:00:55

Open in new window


my SQL
 select  datediff(hour, 0, cast(CallTime as time)),
  DATEADD(ms, SUM(DATEDIFF(ms, '00:00:00.000',  convert(varchar(10), dateadd(second, Duration, 0), 108))), '00:00:00.000') as time
  from NT_API_CallLogs
 WHERE Duration <> 0
 AND cast(CallTime as date) = '2017-3-08'
GROUP BY datediff(hour, 0, cast(CallTime as time) )
ORDER BY datediff(hour, 0, cast(CallTime as time) )

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
cast the result of the call to datediff as a Time value, not the parameter that you're passing.
Chris JonesLead Application Web Developer

Author

Commented:
but this line is the line I need in just hours minutes sand seconds call time returns the correct result i need

this is the line i need to remove the default date from.
  DATEADD(ms, SUM(DATEDIFF(ms, '00:00:00.000',  convert(varchar(10), dateadd(second, Duration, 0), 108))), '00:00:00.000') as time

Open in new window

Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
I think you can just do this and get the desired result, if I'm reading the code correctly:

Select  datediff(hour, 0, cast(CallTime as time)),
  CAST(DATEADD(SECOND, SUM(Duration), 0) AS time) AS time
  from NT_API_CallLogs
 WHERE Duration <> 0
 AND cast(CallTime as date) = '2017-3-08'
GROUP BY datediff(hour, 0, cast(CallTime as time) )
ORDER BY datediff(hour, 0, cast(CallTime as time) )
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Chris JonesLead Application Web Developer

Author

Commented:
AWESOME Scott - am i able to remove the trailing 0's i was thinking to substring it maybe.
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
If you just want HH:MM, you can do this:

CONVERT(varchar(5), CAST(DATEADD(SECOND, SUM(Duration), 0) AS time), 8) AS time

If you want HH:MM:SS, change to "varchar(8)".
Chris JonesLead Application Web Developer

Author

Commented:
Thanks this worked great

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial