Convert value to seconds

  • Hello,

    I want to calculate total sum of column.

    I have a table with many values of column:

    declare @time table

    (times nvarchar(255))

    INSERT INTO @time (times)

    Select ('05:01')

    UNION ALL

    Select ('00:01')

    UNION ALL

    Select ('170:01')

    UNION ALL

    Select ('2543:01')

    Select * from @time

    How can I get seconds value for every row?

    05:01 - 301

    00:01 - 1

    170:01 - 10201

    P.s. Finally I use - Select convert(char,dateadd(second,152581,60),108) as sumtime

    Can somebody help me?

  • Myke85 (6/3/2014)


    Hello,

    I want to calculate total sum of column.

    I have a table with many values of column:

    declare @time table

    (times nvarchar(255))

    INSERT INTO @time (times)

    Select ('05:01')

    UNION ALL

    Select ('00:01')

    UNION ALL

    Select ('170:01')

    UNION ALL

    Select ('2543:01')

    Select * from @time

    How can I get seconds value for every row?

    05:01 - 301

    00:01 - 1

    170:01 - 10201

    P.s. Finally I use - Select convert(char,dateadd(second,152581,60),108) as sumtime

    Can somebody help me?

    You would do yourself a huge favor if you used proper datatypes for storing your data. Not sure what all this really is but varchar is not the right datatype. Looks like maybe you have minutes and seconds? Put that into two columns with int as the datatype and this is trivial.

    Here is one way you parse this into a single int.

    Select *, (cast(PARSENAME(Replace(t.times, ':', '.'), 2) as int) * 60) + cast(PARSENAME(Replace(t.times, ':', '.'), 1) as int)

    from @time t

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • select times, LEFT(times, len(times) - 3) * 60 + RIGHT(times, 2) as seconds

    from @time

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply