在Facebook中,在每个状态下,我都可以看到创建日期和现在日期之间的时间。防爆。 1小时前,六个月前,1年前,...
因此,在我的数据库SQL中,我想创建一个View,其中包含一个描述creationDate和NowDate之间时间的列--NVARCHAR(30)可能包含字符串:1天前,2个月前等等。 / p>
我有一个比较2 DateTime变量的解决方案,但不清楚:
还有其他解决办法。
答案 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
查询中的数字来自以下的秒数:
不使用秒的简单版本是:
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