2016-12-30 157 views
4

從註冊日期開始計算天數的SQL選擇查詢。我在我的SQL Server 2008數據庫的Registration表中有一列RegistrationDate。從註冊日期開始計算天數的SQL選擇查詢

假設我有一個registrationDate這樣的:

2016-12-10 

和假設今天是2016-12-20,那麼輸出應該是這樣的:

registrationDate  registrationAge  
    2016-12-10    10 days 
+0

提示:'DateDiff' – GurV

+0

我是新的SQL查詢,請能更新你的答案。 @GurwinderSingh – MMK

+0

嘗試SELECT DATEDIFF(dd,column1,column2)FROM MyTable –

回答

2

您可以使用datediff

select registrationDate, 
    datediff(day, cast(registrationDate as date), getdate()) + ' days' as registrationAge 
from table 
+0

您添加了+'天'給予,如果我將刪除天意味着給予天數,你能檢查,錯誤:轉換失敗時將varchar值「days」轉換爲數據類型int。 @Gurwinder Singh – MMK

+0

如果你只是想要整數值,只要丟失+'天'部分 – GurV

+0

不,我也需要那部分。 @Gurwinder Singh – MMK

相關問題