在sql中的where子句中添加月份

时间:2014-12-30 06:13:04

标签: sql where-clause

我正在写一个sp来获取特定日期字段的接下来6个月的数据,

SELECT CR.[Id] AS ClaimId
    ,CR.[BOLNumber]
    ,CR.[PRONumber]
    ,CR.[ClaimNumber]
    ,CR.[CompanyName]
    ,c.NAME AS CarrierName
    ,CR.[DateFiled]
    ,CONVERT(VARCHAR(10),CR.[DateFiled], 103) AS DateFiledString
    ,CR.[ClaimDate] AS ClaimReceivedDate
    ,CONVERT(VARCHAR(10), CR.[ClaimDate], 103) AS ClaimReceivedDateString
    ,CR.[AmountFiled]
    ,CR.[Status] AS StatusId
    ,CR.[SettledAmount]
FROM CarrierRate.Claims AS CR WITH (NOLOCK)
WHERE CR.CustomerId = @AccountUserId AND
 CR.Status = @statusType AND CR.ClaimDate < DATEADD(month,6,CR.ClaimDate)
ORDER BY CR.[Status] ASC

该字段是ClaimDate。我正确地做对了还是要改变什么? 请建议

3 个答案:

答案 0 :(得分:0)

使用BETWEEN关键字指定日期范围。

WHERE CR.CustomerId = @AccountUserId AND
 CR.Status = @statusType AND
 CR.ClaimDate BETWEEN GETDATE() AND DATEADD(month,6,CR.ClaimDate)

答案 1 :(得分:0)

你可以尝试

CONVERT(DATE, CR.ClaimDate) < DATEADD(MONTH,6,CONVERT(DATE, CR.ClaimDate))

此处仅考虑日期。

答案 2 :(得分:0)

 (CR.ClaimDate BETWEEN DATEADD(month,-6,GETDATE()) AND GETDATE()
   OR @statusType <> 2)