我想对当月第一天到当月最后一天之间进行的某些交易执行DELETION。仅当FileUploadDate到达每月的最后一天时,才执行删除过程。换句话说,当FileUploadDate等于该月的最后一天时。这里的主要问题是,当WHERE子句的第一个条件为TRUE时,第二个条件被第一个条件的值“过滤”,这使其不相关。我尝试使用其他方法,例如CASE-WHEN-THEN,但在将DELETE集成到CASE子句中时遇到了问题(如果可能的话)。有没有一种方法可以执行两个单独的条件而又不影响另一个?谢谢。
DELETE
FROM transacions
WHERE EXISTS
(
SELECT
*
FROM transacions
WHERE fileuploaddate = CONVERT( date, dateadd(d, -2, dateadd(m, datediff(m, 0, getdate()) + 1, 0)),103)
AND fileuploaddate BETWEEN CONVERT(date, dateadd(month, datediff(month, 0, getdate()), 0))
AND CONVERT(date, dateadd(d, -1, dateadd(m, datediff(m, 0, getdate()) + 1, 0)),103)
)
答案 0 :(得分:0)
您可以使用OR
代替AND
WHERE FileUploadDate = CONVERT(DATE, DATEADD(d, -2, DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 1, 0)),103)
OR FileUploadDate BETWEEN CONVERT(DATE, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)) AND CONVERT(DATE, DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 1, 0)),103)
答案 1 :(得分:0)
尝试一下:
DELETE
FROM Transactions
WHERE EOMONTH (FileUploadDate) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) -- delete on the last day of the month
AND FileUploadDate >= DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) -- delete files from the begining of the month
AND FileUploadDate <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) -- delete files to the end of the current montn