pre { /*控制代码不换行*/ white-space: pre; word-wrap: normal; }

统计两个时间相隔多少年、月、日、小时、分钟、秒钟(原创)

代码如下:

 

/* 统计两个时间相隔多少年、月、日、小时、分钟、秒钟 */
 
 
 
--查询多久前,返回值可能为:
-- 1分钟前,59分钟前
-- 1小时前,23小数前
-- 1天前,29天前
-- 1月前,11个月前
-- 1年前,10年前
CREATE FUNCTION HowLongTimesBefore ( @beginDateParam datetime, @endDateParam datetime ) Returns varchar(20)
AS
BEGIN
declare @returnString varchar(20)
declare @tempNum int
set @tempNum = datediff(year,@beginDateParam,@endDateParam)
if(@tempNum < 0)
    return convert(varchar(20),abs(@tempNum)) + '年后';
if(@tempNum > 1)
    return convert(varchar(20),@tempNum) + '年前';
/* 执行到这里,说明等于0或者等于1 */
if(@tempNum = 0)
begin
    --同一年
    set @tempNum = datediff(month,@beginDateParam,@endDateParam)
    if(@tempNum < 0)
        return convert(varchar(20),abs(@tempNum)) + '月后';
    if(@tempNum > 1)
        return convert(varchar(20),@tempNum) + '月前';
    /* 执行到这里,说明等于0或者等于1 */
    if(@tempNum = 0)
    begin
        --同月
        set @tempNum = datediff(day,@beginDateParam,@endDateParam)
        if(@tempNum < 0)
            return convert(varchar(20),abs(@tempNum)) + '天后';
        if(@tempNum > 1)
            return convert(varchar(20),@tempNum) + '天前';
        /* 执行到这里,说明等于0或者等于1 */
        if(@tempNum = 0)
        begin
            --同天
            set @tempNum = datediff(hour,@beginDateParam,@endDateParam)
            if(@tempNum < 0)
                return convert(varchar(20),abs(@tempNum)) + '小时后';
            if(@tempNum > 1)
                if(@tempNum >= 24)
                    return '1天前';
                else
                    return convert(varchar(20),@tempNum) + '小时前';
            /* 执行到这里,说明等于0或者等于1 */
            if(@tempNum = 0)
            begin
                --同一个小时
                set @tempNum = datediff(minute,@beginDateParam,@endDateParam)
                if(@tempNum < 0)
                    return convert(varchar(20),abs(@tempNum)) + '分钟后';
                if(@tempNum > 1)
                    return convert(varchar(20),@tempNum) + '分钟前';
                /* 执行到这里,说明等于0或者等于1 */
                set @tempNum = datediff(second,@beginDateParam,@endDateParam)
                return convert(varchar(20),@tempNum) + '秒前';
            end
            else
            begin
                --跨小时
                set @tempNum = datediff(minute,@beginDateParam,@endDateParam)
                return convert(varchar(20),@tempNum) + '分钟前';
            end
        end
        else
        begin
            --跨天
            set @tempNum = datediff(hour,@beginDateParam,@endDateParam)
            if(@tempNum >= 24)
                return '1天前';
            else
                return convert(varchar(20),@tempNum) + '小时前';
        end
    end
    else
    begin
        --跨月,相邻的两个月
        set @tempNum = datediff(day,@beginDateParam,@endDateParam)
        if(@tempNum > 1)
            return convert(varchar(20),@tempNum) + '天前';
        else
        begin
            --30号与1号,或者是31号与1号
            set @tempNum = datediff(hour,@beginDateParam,@endDateParam)
            if(@tempNum >= 24)
                return '1天前';
            else
                return convert(varchar(20),@tempNum) + '小时前';
        end
    end
end
else
begin
    --跨年
    set @tempNum = datediff(month,@beginDateParam,@endDateParam)
    if(@tempNum > 1)
        return convert(varchar(20),@tempNum) + '月前';
    /* 执行到这里,说明等于1 */
    else
    begin
        --跨月,12月与1月
        set @tempNum = datediff(day,@beginDateParam,@endDateParam)
        if(@tempNum > 1)
            return convert(varchar(20),@tempNum) + '天前';
        else
        begin
            --相隔一条,说明是12月31号与1月1号
            set @tempNum = datediff(hour,@beginDateParam,@endDateParam)
            if(@tempNum >= 24)
                return '1天前';
            else
                return convert(varchar(20),@tempNum) + '小时前';
        end
    end
end
return @returnString
END
go
 
--测试
select dbo.HowLongTimesBefore('2010-10-25 12:00:30','2010-10-25 12:02:00')
 
转自:http://www.cnblogs.com/Music/archive/2010/08/14/SQLHowLongTimesBefore.html
posted @ 2012-03-17 00:52  monkey's  阅读(684)  评论(0)    收藏  举报