ivka ivka - 4 years ago 100
SQL Question

TIme - SQL (Minutes and Seconds)

I created a TIME table. This table has two columns: one for minutes and another one for seconds. I made their datatype as a Decimal.

Is there a way to create a derivative column where minutes and seconds are in this format mm:ss from my two columns?

IF NOT, How do I insert data into my minute column if its not a DECIMAL type? What type should it be?

Thank you!

Note I am using SQL server

Answer Source

As @mclaassen pointed out, I'd be curious why you aren't using the built in time data type.

That said, if you really want to build a time table by hand, then you can have a calculated column. Let's call it timeString.

alter table [time] 
    add timeString as (left('0' + cast([minutes] as varchar(10)), 2) + ':' + left('0' + cast([seconds] as varchar(10)), 2))

See https://msdn.microsoft.com/en-us/library/ms188300.aspx for documentation on calculated columns in SQL Server.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download