计算SQL Server 2008中CreationDate和Now Date之间的时间

时间:2013-11-03 02:43:27

标签: sql sql-server sql-server-2008

在Facebook中,在每个状态下,我都可以看到创建日期和现在日期之间的时间。防爆。 1小时前,六个月前,1年前,...

因此,在我的数据库SQL中,我想创建一个View,其中包含一个描述creationDate和NowDate之间时间的列--NVARCHAR(30)可能包含字符串:1天前,2个月前等等。 / p>

我有一个比较2 DateTime变量的解决方案,但不清楚:

  1. 我将计算DateCreation和Now之间的时间作为TimeInterval
  2. 如果TimeInterval< 1分钟 - > “Just Now”,如果TimeInterval> = 1min且< 60min - >计算2个TimePoints之间的分钟。假设6分钟 - > “6分钟前”
  3. 同样,持续数月或数年。
  4. 还有其他解决办法。

2 个答案:

答案 0 :(得分:2)

您可以使用与以下类似的代码生成数据。几个月不太精确,因为我用了一个月30天。多年来,我每年使用365天。但我想这应该足够精确。

declare @date1 as datetime = '20131103 13:00:40'
declare @date2 as datetime = '20160208 16:40:45'

select case 
    when datediff(ss, @date1, @date2) < 60 THEN cast(datediff(ss, @date1, @date2) as varchar(max)) + ' seconds ago'
    when datediff(ss, @date1, @date2) < 3600 THEN cast((datediff(ss, @date1, @date2) / 60) as varchar(max)) + ' minutes ago'
    when datediff(ss, @date1, @date2) < 86400 THEN cast((datediff(ss, @date1, @date2) / 3600) as varchar(max)) + ' hours ago'
    when datediff(ss, @date1, @date2) < 2592000 THEN cast((datediff(ss, @date1, @date2) / 86400) as varchar(max)) + ' days ago'
    when datediff(ss, @date1, @date2) < 31536000 THEN cast((datediff(ss, @date1, @date2) / 2592000) as varchar(max)) + ' months ago'
    else cast((datediff(ss, @date1, @date2) / 31536000) as varchar(max)) + ' years ago'
end

查询中的数字来自以下的秒数:

  • 分钟= 60
  • 小时= 3600
  • day = 86400
  • month = 2592000
  • year = 31536000

不使用秒的简单版本是:

declare @date1 as datetime = '20131003 13:00:40'
declare @date2 as datetime = '20151101 16:40:45'

select case 
    when datediff(ss, @date1, @date2) < 60 THEN cast(datediff(ss, @date1, @date2) as varchar(max)) + ' seconds ago'
    when datediff(mi, @date1, @date2) < 60 THEN cast((datediff(mi, @date1, @date2)) as varchar(max)) + ' minutes ago'
    when datediff(hh, @date1, @date2) < 24 THEN cast((datediff(hh, @date1, @date2)) as varchar(max)) + ' hours ago'
    when datediff(dd, @date1, @date2) < 30 THEN cast((datediff(dd, @date1, @date2)) as varchar(max)) + ' days ago'
    when datediff(mm, @date1, @date2) < 13 THEN cast((datediff(mm, @date1, @date2)) as varchar(max)) + ' months ago'
    else cast((datediff(yy, @date1, @date2)) as varchar(max)) + ' years ago'
end

答案 1 :(得分:1)

我会把它放在像这样的函数中:

 create function dbo.TimeDiffToWords(@eventDateTime as DateTime)
    returns varchar(100)
    as 
    begin
    declare @ret varchar(100)
    declare @value int
    declare @done bit

set @done = 0

set @value = datediff(year, @eventDateTime, getdate())

if @value > 0
begin
    set @ret = 'Over '+cast(@value as varchar(6))+' year(s) ago.'
    set @done = 1
end

if @done = 0
begin
  set @value = datediff(month, @eventDateTime, getdate())
    if @value > 0
    begin
        set @ret = 'Over '+cast(@value as varchar(6))+' months(s) ago.'
        set @done = 1
    end
end

if @done = 0
begin
  set @value = datediff(day, @eventDateTime, getdate())
    if @value > 0
    begin
        set @ret = 'Over '+cast(@value as varchar(6))+' days(s) ago.'
        set @done = 1
    end
end

-- continue till down to seconds ago.
if @done = 0
begin
    set @ret = 'Not coded yet.'
end

return @ret

end

然后你可以像测试代码一样调用它:

select dbo.TimeDiffToWords(EventDateTime)
from (select dateadd(month,-40, getdate()) as EventDateTime
    union all select dateadd(day,-40, getdate())
    union all select dateadd(day,-6, getdate())
    union all select dateadd(day,-2, getdate())
    union all select dateadd(hour,-4, getdate())
    union all select dateadd(minute,-2, getdate())
) as testTimes