Home > Enterprise >  SQL Server convert varchar hh:mm:ss to time
SQL Server convert varchar hh:mm:ss to time

Time:12-04

Is there a way to convert varchar 'hh:mm:ss' format data to time to allowing summing?

CodePudding user response:

DECLARE @InX VARCHAR(10)='09:08:23'
SELECT CAST(@InX AS TIME(0))

CodePudding user response:

You can convert those values to time and for summing you could simply get their difference in seconds from midnight and sum. Result would be in seconds. ie:

DECLARE @times TABLE(t TIME(0));
INSERT INTO @times(t)VALUES('03:20:30'), ('02:10'), ('01:00');

SELECT SUM(DATEDIFF(SECOND, '00:00:00', t))FROM @times AS t;
  • Related