There is a column named as duration in a table called Adventurous.The column has values as below.In the column Suffix of 'H' is hours,Suffix of 'M' is minutes and Suffix of 'S' is seconds.How can we select the hours, minutes and seconds and convert all into seconds i.e sum of all the hours minutes and seconds in the form of seconds.
Duration
--------
PT10M13S
PT13M22S
PT1H2M18S
PT11S
i tried using substring and charindex as below and tried to create a function but i am getting error:
Declare @Duration varchar(30) ='PT16H13M42S', @Dur varchar(10)
Declare @hours int
declare @mins int
declare @secs int
declare @len int
select @len = len(substring (@Duration, 3, len(@Duration))), @Dur=substring (@Duration, 3, len(@Duration))
select @hours = charindex('H', @Dur)
select substring(@Dur, 1, @hours-1)
select @Duration=substring (@Dur, @hours+1, len(@Dur))
select @mins = charindex('M', @Duration)
select substring(@Duration, 1, @mins-1)
select @Dur=substring (@Duration, @mins+1, len(@Duration))
select @secs= charindex('S', @Dur)
select substring(@Dur, 1, @Secs-1)
select @len, @Dur, @Duration
example PT1H2M18S= 1*3600+2*60+18=3738