我需要选择所有不超过18个月的记录。我为SQL Server编写了这个查询。但它似乎没有用。
SELECT DISTINCT Company,
Name,
PhoneNumber,
FROM Request
WHERE Company LIKE @Company
AND RequestDate >= ADD_MONTHS(SYSDATE, -18)
答案 0 :(得分:4)
SELECT DISTINCT Company,
Name,
PhoneNumber,
FROM Request
WHERE Company LIKE @Company
AND datediff(mm, RequestDate, getdate()) <= 18
使用DATEDIFF
function进行此操作。以上查询可以为您提供所需的结果。
答案 1 :(得分:3)
SELECT DISTINCT Company, Name, PhoneNumber,
FROM Request WHERE Company LIKE @Company
AND RequestDate >= DATEADD(MONTH, -18, GETDATE())