Calculate time difference-scalar value function

1. Calculate the time difference between the two times (hours: minutes: seconds)

- ============================================= 
- program name: GetMCBTimeDifferenceByDateTime 
- Create time: 2017-07-18 
- author: PxQ 
- function: calculate the time difference between the two times (hours: minutes: seconds) 
- example: select [dbo] .GetMCBTimeDifferenceByDateTime_New ( ' 2018-01- 01 10:59:30 ',' 2018-01-02 12:30:20 ') --25: 30: 50 
- ==================== ========================= 
the CREATE  the FUNCTION  [ the dbo ] . [ GetMCBTimeDifferenceByDateTime_New ]   
( 
    @MCBSheetDateTime  datetime , - time 
    @NowTime  datetime  - comparison time 
)
The RETURNS  NVARCHAR ( MAX )
 the AS 
the BEGIN 
    - defined field name needs to return 
    DECLARE  @ReturnName  nvarchar ( max ) 

     DECLARE  @day  int   
    DECLARE  @hour  int   
    DECLARE  @min  int   
    DECLARE  @sec  int   
    DECLARE  @alls  int   
    SET  @alls = DATEDIFF (S, @MCBSheetDateTime , @NowTime )    - the difference between the time and the time 
    SET  @day = @alls / 86400   
    SET @hour=(@alls-@day*86400)/3600  
    set @min=(@alls-@day*86400-@hour*3600)/60  
    set @sec=@alls-@day*86400-@hour*3600-@min*60  
    set @ReturnName=(select (case when convert(nvarchar(10), (@day*24+@hour)) is null then '' else convert(nvarchar(10), (@day*24+@hour)) end ))
    set @hour=@day*24+@hour;
    --返回天时分秒
    select @ReturnName=(case when len(@hour)>1 then (cast(@hour as varchar(5))) else ('0'+cast(@hour as varchar(5))) end) +':'+
    (case when len (@min)>1 then CAST(@min as varchar(2)) else ('0'+CAST(@min as varchar(2))) end)
    +':'+
    (case when len (@sec)>1 then CAST(@sec as varchar(2)) else ('0'+CAST(@sec as varchar(2))) end)
    +''
    RETURN @ReturnName
END

Calculate the time difference between two times (hour: minute: second)
Calculate the time difference from the present (hour: minute: second)

 

2. Calculate the time difference from the present (minutes)

- ============================================= 
- program name: GetTimeDifferenceByDateTime 
- Create time: 2017-07-18 
- author: PxQ 
- function: the current time difference (min) to calculate the distance 
- example: select [dbo] .GetTimeDifferenceByDateTime (( select '2019-02-27 10 : 59 ')) --29 
- ============================================ ===== 
the CREATE  the FUNCTION  [ the dbo ] . [ GetTimeDifferenceByDateTime ]   
( 
    @datetime  datetime  - time 
)
 the RETURNS  NVARCHAR ( MAX )
 the AS 
the BEGIN 
    -Define the field names to be returned 
    declare  @ReturnName  nvarchar ( max ) 

     declare  @day  int   
    declare  @hour  int   
    declare  @min  int   
    declare  @sec  int   
    declare  @alls  int   
    set  @alls = datediff (s, @DateTime , GETDATE ())    - and the time difference between the time 
    SET  @day = @alls / 86400   
    SET  @hour = ( @alls - @day * 86400 )/3600  
    set @min=(@alls-@day*86400-@hour*3600)/60  
    set @sec=@alls-@day*86400-@hour*3600-@min*60  
    --返回分钟
    set @ReturnName=(select (case when convert(nvarchar(50), ((@day*24+@hour)*60)+@min) is null then '' else convert(nvarchar(50), ((@day*24+@hour)*60)+@min) end ))

    RETURN @ReturnName

END
Calculate the time difference from the present (minutes)

 

Guess you like

Origin www.cnblogs.com/panxueqin/p/12736790.html