Technical Article

Calculate Time - Most Advanced UOC

,

This function is used to calculate the actual unit of conversion for time.

This is a most advanced generic function , which suppose to return values for 42 combinations.

Regards,

Vignesh Arulmani

create function fn_calc_uoc_time(@FromConversionUnit varchar(64),@ToConversionUnit varchar(64),@UnitValue float)
returns float
as
begin

--TIME


declare @returnvalue float

select @returnvalue = 
case 
when @FromConversionUnit = 'day' and @ToConversionUnit = 'hour' then @UnitValue * 24.0
when @FromConversionUnit = 'day' and @ToConversionUnit = 'microsecond' then @UnitValue * 86400000000.0
when @FromConversionUnit = 'day' and @ToConversionUnit = 'millisecond' then @UnitValue * 86400000.0
when @FromConversionUnit = 'day' and @ToConversionUnit = 'minute' then @UnitValue * 1440.0
when @FromConversionUnit = 'day' and @ToConversionUnit = 'second' then @UnitValue * 86400.0
when @FromConversionUnit = 'day' and @ToConversionUnit = 'week' then @UnitValue * 0.1428571428571429

when @FromConversionUnit = 'hour' and @ToConversionUnit = 'day' then @UnitValue * 0.0416666666666667
when @FromConversionUnit = 'hour' and @ToConversionUnit = 'microsecond' then @UnitValue * 3600000000.0
when @FromConversionUnit = 'hour' and @ToConversionUnit = 'millisecond' then @UnitValue * 3600000.0
when @FromConversionUnit = 'hour' and @ToConversionUnit = 'minute' then @UnitValue * 60.0
when @FromConversionUnit = 'hour' and @ToConversionUnit = 'second' then @UnitValue * 3600.0
when @FromConversionUnit = 'hour' and @ToConversionUnit = 'week' then @UnitValue * 0.005952380952381

when @FromConversionUnit = 'microsecond' and @ToConversionUnit = 'day' then @UnitValue * 1.157407407407407e-11
when @FromConversionUnit = 'microsecond' and @ToConversionUnit = 'hour' then @UnitValue * 2.777777777777778e-10
when @FromConversionUnit = 'microsecond' and @ToConversionUnit = 'millisecond' then @UnitValue * 0.001
when @FromConversionUnit = 'microsecond' and @ToConversionUnit = 'minute' then @UnitValue * 1.666666666666667e-8
when @FromConversionUnit = 'microsecond' and @ToConversionUnit = 'second' then @UnitValue * 0.000001
when @FromConversionUnit = 'microsecond' and @ToConversionUnit = 'week' then @UnitValue * 1.653439153439153e-12

when @FromConversionUnit = 'millisecond' and @ToConversionUnit = 'day' then @UnitValue * 1.157407407407407e-8
when @FromConversionUnit = 'millisecond' and @ToConversionUnit = 'hour' then @UnitValue * 2.777777777777778e-7
when @FromConversionUnit = 'millisecond' and @ToConversionUnit = 'microsecond' then @UnitValue * 1000.0
when @FromConversionUnit = 'millisecond' and @ToConversionUnit = 'minute' then @UnitValue * 1.666666666666667e-5
when @FromConversionUnit = 'millisecond' and @ToConversionUnit = 'second' then @UnitValue * 0.001
when @FromConversionUnit = 'millisecond' and @ToConversionUnit = 'week' then @UnitValue * 1.653439153439153e-9

when @FromConversionUnit = 'minute' and @ToConversionUnit = 'day' then @UnitValue * 6.944444444444444e-4
when @FromConversionUnit = 'minute' and @ToConversionUnit = 'hour' then @UnitValue * 0.0166666666666667
when @FromConversionUnit = 'minute' and @ToConversionUnit = 'microsecond' then @UnitValue * 60000000.0
when @FromConversionUnit = 'minute' and @ToConversionUnit = 'millisecond' then @UnitValue * 60000.0
when @FromConversionUnit = 'minute' and @ToConversionUnit = 'second' then @UnitValue * 60.0
when @FromConversionUnit = 'minute' and @ToConversionUnit = 'week' then @UnitValue * 9.920634920634921e-5

when @FromConversionUnit = 'second' and @ToConversionUnit = 'day' then @UnitValue * 1.157407407407407e-5
when @FromConversionUnit = 'second' and @ToConversionUnit = 'hour' then @UnitValue * 2.777777777777778e-4
when @FromConversionUnit = 'second' and @ToConversionUnit = 'microsecond' then @UnitValue * 1000000.0
when @FromConversionUnit = 'second' and @ToConversionUnit = 'millisecond' then @UnitValue * 1000.0
when @FromConversionUnit = 'second' and @ToConversionUnit = 'minute' then @UnitValue * 0.0166666666666667
when @FromConversionUnit = 'second' and @ToConversionUnit = 'week' then @UnitValue * 1.653439153439153e-6

when @FromConversionUnit = 'week' and @ToConversionUnit = 'day' then @UnitValue * 7.0
when @FromConversionUnit = 'week' and @ToConversionUnit = 'hour' then @UnitValue * 168.0
when @FromConversionUnit = 'week' and @ToConversionUnit = 'microsecond' then @UnitValue * 604800000000.0
when @FromConversionUnit = 'week' and @ToConversionUnit = 'millisecond' then @UnitValue * 604800000.0
when @FromConversionUnit = 'week' and @ToConversionUnit = 'minute' then @UnitValue * 10080.0
when @FromConversionUnit = 'week' and @ToConversionUnit = 'second' then @UnitValue * 604800.0
 
when @FromConversionUnit = @ToConversionUnit then @UnitValue * 1.0
end

return(@returnvalue)

end
go


--examples
select dbo.fn_calc_uoc_time('day','hour',10)
select dbo.fn_calc_uoc_time('day','microsecond',10)
select dbo.fn_calc_uoc_time('day','millisecond',10)
select dbo.fn_calc_uoc_time('day','minute',10)
select dbo.fn_calc_uoc_time('day','second',10)
select dbo.fn_calc_uoc_time('day','week',10)

select dbo.fn_calc_uoc_time('hour','day',10)
select dbo.fn_calc_uoc_time('hour','microsecond',10)
select dbo.fn_calc_uoc_time('hour','millisecond',10)
select dbo.fn_calc_uoc_time('hour','minute',10)
select dbo.fn_calc_uoc_time('hour','second',10)
select dbo.fn_calc_uoc_time('hour','week',10)

select dbo.fn_calc_uoc_time('microsecond','day',10)
select dbo.fn_calc_uoc_time('microsecond','hour',10)
select dbo.fn_calc_uoc_time('microsecond','millisecond',10)
select dbo.fn_calc_uoc_time('microsecond','minute',10)
select dbo.fn_calc_uoc_time('microsecond','second',10)
select dbo.fn_calc_uoc_time('microsecond','week',10)

select dbo.fn_calc_uoc_time('millisecond','day',10)
select dbo.fn_calc_uoc_time('millisecond','hour',10)
select dbo.fn_calc_uoc_time('millisecond','microsecond',10)
select dbo.fn_calc_uoc_time('millisecond','minute',10)
select dbo.fn_calc_uoc_time('millisecond','second',10)
select dbo.fn_calc_uoc_time('millisecond','week',10)

select dbo.fn_calc_uoc_time('minute','day',10)
select dbo.fn_calc_uoc_time('minute','hour',10)
select dbo.fn_calc_uoc_time('minute','microsecond',10)
select dbo.fn_calc_uoc_time('minute','millisecond',10)
select dbo.fn_calc_uoc_time('minute','second',10)
select dbo.fn_calc_uoc_time('minute','week',10)

select dbo.fn_calc_uoc_time('second','day',10)
select dbo.fn_calc_uoc_time('second','hour',10)
select dbo.fn_calc_uoc_time('second','microsecond',10)
select dbo.fn_calc_uoc_time('second','millisecond',10)
select dbo.fn_calc_uoc_time('second','minute',10)
select dbo.fn_calc_uoc_time('second','week',10)

select dbo.fn_calc_uoc_time('week','day',10)
select dbo.fn_calc_uoc_time('week','hour',10)
select dbo.fn_calc_uoc_time('week','microsecond',10)
select dbo.fn_calc_uoc_time('week','millisecond',10)
select dbo.fn_calc_uoc_time('week','minute',10)
select dbo.fn_calc_uoc_time('week','second',10)

Rate

1.43 (7)

You rated this post out of 5. Change rating

Share

Share

Rate

1.43 (7)

You rated this post out of 5. Change rating