我有查询:
SELECT name FROM ( SELECT name FROM Hist_answer WHERE id_city='34324' AND datetime >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH) UNION ALL SELECT name FROM Hist_internet WHERE id_city='34324' AND datetime >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH) ) x GROUP BY name ORDER BY name
但是 DATE_SUB 是一个MySQL函数,我需要MsSQL 2008的函数
请告诉我如何使用MsSQL 2008从30天中选择数据?
PS: 日期 时间的数据类型 是smalldatetime
smalldatetime
您应该使用的DATEADD是Sql服务器,因此,如果尝试此简单选择,您将看到影响
DATEADD
Select DATEADD(Month, -1, getdate())
结果
2013-04-20 14:08:07.177
在您的情况下,请尝试此查询
SELECT name FROM ( SELECT name FROM Hist_answer WHERE id_city='34324' AND datetime >= DATEADD(month,-1,GETDATE()) UNION ALL SELECT name FROM Hist_internet WHERE id_city='34324' AND datetime >= DATEADD(month,-1,GETDATE()) ) x GROUP BY name ORDER BY name