2016-03-04 117 views
0

我有一個字段lastLogin這是datetime。 我想告訴用戶他/她的最後一次登錄時間爲
N年前,如果是不到1比今年
N個月前,如果是少的1個月比
ñ日前,如果是不到1比前一交易日
n個小時前,如果是不到1一刻比
n分鐘前,如果不到1分鐘比
n秒前SQL Server將日期時間轉換爲n期前

我想的是,小號在年,月,日,小時,minures和秒爲英語語法的規則自動處理。

我在SQL一個完整的初學者,我使用SQL Server 2008 R2的

+2

這看起來很像家庭作業。你有什麼嘗試?這真的不是太複雜,但代碼將會滿足這些要求。你需要做大量的數學和表達式。 –

+0

非常真實,這是很乏味的工作,但對你來說很簡單。請發送簡短的腳本以瞭解如何繼續。如果其他就足夠了。我還沒有做任何事情,但對於小代碼,你會幫我一個忙, –

+2

這聽起來像一個演示文稿的關注,因此這在客戶端應用程序方面做得更好。 –

回答

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。該函數的定義可以遵循與上述相同的模型 - 插入IIF或IF THEN ELSE語句。

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 

未經測試並書面匆忙作晚飯準備好了!

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