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) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

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

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