时间差函数使用

IF OBJECT_ID('[dbo].[get_service_status]') IS NOT NULL
   DROP FUNCTION [dbo].[get_service_status]
GO

CREATE FUNCTION [dbo].[get_service_status]
(
    @arrear_time    datetime,
    @reminder_time  int,       (单位:天)
    @buffer_time    int            (单位:天)
)
RETURNS nvarchar(10)
AS
BEGIN
    DECLARE @service_status nvarchar(10)
    SET @service_status = '状态1'
    
    -- 当前时间
    DECLARE @current_time datetime
    --SET @current_time = getdate()  -- sqlserver2000中不能这样调用
    SELECT @current_time = Today FROM [dbo].[view_get_date]

    -- 时间差
    DECLARE @nDiffDay int
    SELECT @nDiffDay = datediff(dd, @current_time, @arrear_time)
    
    IF (@nDiffDay > @reminder_time)
    BEGIN
       SET @service_status = '状态1'
    END
    ELSE IF (@nDiffDay <= @reminder_time AND @nDiffDay >= 0)
    BEGIN
       SET @service_status = '状态2'
    END
    ELSE IF (@nDiffDay < 0 AND @nDiffDay >= -@buffer_time)
    BEGIN
       SET @service_status = '状态3'
    END
    ELSE 
    BEGIN
       SET @service_status = '状态4'
    END
    
    RETURN @service_status
END
GO


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值