SQL Server将日期时间转换为n期前

时间:2016-03-04 17:45:38

标签: sql sql-server datetime

我有一个 lastLogin 字段,即datetime。 我想向用户显示他/她的上次登录时间为
n年前,如果少于1年 n个月前,如果少于1个月 n天前,如果它比第1天少 n小时前,如果小于1小时 n分钟前,如果小于1分钟 n秒前

我希望将年,月,日,小时,小时和秒中的 s 作为英语语法规则自动处理。

我是sql的完全初学者,我正在使用 SQL SERVER 2008 R2

3 个答案:

答案 0 :(得分:1)

您可以使用嵌套的IIF()函数并使用DATEDIFF()和GETDATE()来执行此操作。

SELECT 
    IFF(DATEDIFF(second, lastLogin, GETDATE()) < 60, 
        CONCAT(DATEDIFF(second, lastLogin, GETDATE()), ' seconds ago'),
        IFF(DATEDIFF(minute, lastLogin, GETDATE()) < 60, 
            CONCAT(DATEDIFF(minute, lastLogin, GETDATE()), ' minutes ago'),
            IIF( .....continue with the nested conditions until years....) 
        )
    ) AS lastloginstring
FROM ..... 

正如您所看到的,查询将变得冗长而笨拙。在视图级别上做它仍然更好。

您可以考虑使用CREATE FUNCTION定义您的函数,该函数将接受DATETIME值作为参数并返回所需的字符串。然后在SELECT语句中,您将只使用SELECT yourfunctionname(lastLogin)FROM。函数的定义可以遵循与上面相同的模型 - nedted IIFs或IF THEN ELSE语句。

答案 1 :(得分:1)

您可以使用CASE声明

CASE
    WHEN DATEDIFF(yy, lastLogin, GETDATE()) > 1 THEN CASE(DATEDIFF(yy, lastLogin, GETDATE()) AS varchar(5) + ' years ago'
    WHEN DATEDIFF(y, lastLogin, GETDATE()) = 1 THEN '1 year ago'
    -- ... and so on
END AS LastLoginPeriod

晚餐准备就绪时,没有经过测试和书写!

答案 2 :(得分:0)

ALTER FUNCTION fngettimeinagoformat(@givenDate DateTime,@curDate DateTime)
RETURNS Varchar(100)
AS
BEGIN
  DECLARE @Date as Varchar(100)
  SELECT @Date = 
  CASE
    WHEN DATEDIFF(ss,@givenDate,@curDate) <= 1 THEN '1 Sec ago'
    WHEN DATEDIFF(ss,@givenDate,@curDate) > 1 AND DATEDIFF(ss,@givenDate,@curDate) <= 60 THEN CONVERT(Varchar,DATEDIFF(ss,@givenDate,@curDate)) + ' Sec ago'
    WHEN DATEDIFF(mi,@givenDate,@curDate) <= 1 THEN '1 min ago'
    WHEN DATEDIFF(mi,@givenDate,@curDate) > 1 AND DATEDIFF(mi,@givenDate,@curDate) <= 60 THEN CONVERT(Varchar,DATEDIFF(mi,@givenDate,@curDate)) + ' mins ago'
    WHEN DATEDIFF(hh,@givenDate,@curDate) <= 1 THEN CONVERT(Varchar,DATEDIFF(hh,@givenDate,@curDate)) + ' hour ago'
    WHEN DATEDIFF(hh,@givenDate,@curDate) > 1 AND DateDiff(hh,@givenDate,@curDate) <= 24 THEN CONVERT(Varchar,DATEDIFF(hh,@givenDate,@curDate)) + ' hrs ago'
    WHEN DATEDIFF(dd,@givenDate,@curDate) <= 1 THEN CONVERT(Varchar,DATEDIFF(dd,@givenDate,@curDate)) + ' day ago'
    WHEN DATEDIFF(dd,@givenDate,@curDate) > 1 AND  DATEDIFF(dd,@givenDate,@curDate) <= 7 THEN CONVERT(Varchar,DATEDIFF(dd,@givenDate,@curDate)) + ' days ago'
    WHEN DATEDIFF(ww,@givenDate,@curDate) <= 1 THEN CONVERT(Varchar,DATEDIFF(ww,@givenDate,@curDate)) + ' week ago'
    WHEN DATEDIFF(ww,@givenDate,@curDate) > 1 AND DATEDIFF(ww,@givenDate,@curDate) <= 4 THEN CONVERT(Varchar,DATEDIFF(ww,@givenDate,@curDate)) + ' weeks ago'
    WHEN DATEDIFF(mm,@givenDate,@curDate) <= 1 THEN CONVERT(Varchar,DATEDIFF(mm,@givenDate,@curDate)) + ' month ago'
    WHEN DATEDIFF(mm,@givenDate,@curDate) > 1 AND DATEDIFF(mm,@givenDate,@curDate) <= 12 THEN CONVERT(Varchar,DATEDIFF(mm,@givenDate,@curDate)) + ' mnths ago'
    WHEN DATEDIFF(yy,@givenDate,@curDate) <= 1 THEN CONVERT(Varchar,DATEDIFF(yy,@givenDate,@curDate)) + ' year ago'
    WHEN DATEDIFF(yy,@givenDate,@curDate) > 1 THEN CONVERT(Varchar,DATEDIFF(yy,@givenDate,@curDate)) + ' yrs ago'
  END
  RETURN @Date
END