Field format for saving time

3

What field should I use in SQL Server to save a running time? Example: 2Days 23H 47Min 00Sec.

If you have an example with Fluent API it will help a lot.

    
asked by anonymous 23.06.2016 / 04:20

2 answers

3

The correct type of .Net to represent time durations is the TimeSpan .

You can use Time to save this data, according to this Compatibility Table .

Something people usually do is also use a INT or BIGINT to save the ticks or milliseconds or even seconds , according to the precision you need.

As the question does not give details, I can not go beyond this.

    
23.06.2016 / 05:14
0

To store a time count that is less than 24 hours, you can use TIME. But if the accumulated exceeds 24 hours, as you cite, then you can use either datetime or int.

When using the datetime type, you must start with 0. Then, add / subtract the hours, minutes, and seconds. At any time, to display the value in hhh: mm format, you can use the

   (valor / 60) + ':' + (valor % 60)

To evaluate what happens when using datetime:

-- código #1
declare @Horas datetime;

-- zera a variável
set @Horas= 0;
PRINT @Horas

-- soma 40 horas
set @Horas= DateAdd(hour, +40, @Horas);
PRINT @Horas

-- soma 38 minutos
set @Horas= DateAdd(minute, +38, @Horas);
PRINT @Horas

-- subtrai 128 minutos
set @Horas= DateAdd(minute, -128, @Horas);
PRINT @Horas

-- exibe no formato hhh:mm
PRINT Cast(DateDiff(minute, 0, @Horas) / 60 as varchar(10)) + 'h' + Right('00' + Cast(DateDiff(minute, 0, @Horas) % 60 as varchar(2)), 2) + 'min'

But a column of type int takes up less space than one of type datetime. So it seems to me that the best implementation remains with a column of type INT.

    
31.10.2016 / 15:51