在这里,我构建了一个查询,用于检索从今天和11个月前过期的政策过期日期。
由于此查询确实检索数据,因此我需要更改查询以从1年前的当前日期和之前一年检索数据。
例如,我需要在2013年5月19日到2014年5月19日之间过期的政策。
我正在尝试使用函数调用,但我不确定如何实现它。
以下是我目前掌握的当前数据:
Customer Number| Name | EMail | Producer | Policy Expired
abcde-12345-fghij-67890 | Bob Builder | somewhere@email.com | 31000 | Dora Explorer | 2014-07-29
abcde-12345-fghij-67890 | Bob Builder | somewhere@email.com | 31000 | Dora Explorer | 2014-08-07
abcde-12345-fghij-67890 | Bob Builder | somewhere@email.com | 31000 | Dora Explorer | 2014-08-10
SELECT DISTINCT
Customer.custno as [Customer Number],
CONCAT(Customer.FirstName, ' ', Customer.LastName) AS Name,
Customer.EMail,
(isnull(Employee.Firstname + ' ','') + isnull(Employee.LastName,'')) AS Producer,
BasicPolInfo.polexpdate as [Policy Expired]
FROM
Customer INNER JOIN
BasicPolInfo ON Customer.CustId = BasicPolInfo.CustId INNER JOIN
Transaction ON BasicPolInfo.PolId = Transaction.PolId INNER JOIN
GeneralLedgerBranch ON Customer.GLBrnchCode = GeneralLedgerBranch.GLBrnchCode INNER JOIN
GeneralLedgerDepartment ON Customer.GLDeptCode = GeneralLedgerDepartment.GLDeptCode INNER JOIN
GeneralLedgerDivision ON Customer.GLDivCode = GeneralLedgerDivision.GLDivCode INNER JOIN
Employee ON BasicPolInfo.ExecCode = Employee.EmpCode
WHERE
Customer.firstname IS NOT NULL
AND Customer.EMail IS NOT NULL
AND Customer.Active = 'Y'
AND Customer.typecust = 'P'
AND BasicPolInfo.PolExpDate >= DATEADD(MONTH, -10,CONVERT(VARCHAR(11),GETDATE(),106))
AND BasicPolInfo.PolExpDate <= CONVERT(VARCHAR(11),GETDATE(),106)
AND BasicPolInfo.status <> 'D'
AND GeneralLedgerDepartment.Name = 'Personal -'
ORDER BY BasicPolInfo.PolExpDate ASC
上面,我正在尝试翻译
AND BasicPolInfo.PolExpDate&gt; = DATEADD(MONTH,-10,CONVERT(VARCHAR(11),GETDATE(),106))
AND BasicPolInfo.PolExpDate&lt; = CONVERT(VARCHAR(11),GETDATE(),106)
进入2013年5月19日至2014年5月19日之间。我该怎么做呢?
我在线查看但无法得出结论。
答案 0 :(得分:2)
如果BasicPolInfo.PolExpDate是DATE数据类型,那么它就像
一样简单AND BasicPolInfo.PolExpDate BETWEEN
DATEADD(YEAR, -2, GETDATE())
AND DATEADD(YEAR, -1, GETDATE())
如果您的数据类型是DATETIME,并且您需要删除时间部分,请使用
AND BasicPolInfo.PolExpDate BETWEEN
DATEADD(YEAR, -2, DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())))
AND DATEADD(YEAR, -1, DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())))