2016-12-30 163 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

相关问题