SQL日期应该是12/20/18,它应该是12/20/17

时间:2018-05-25 19:21:08

标签: sql date dateadd datepart

任何帮助将不胜感激。它适用于2/10 / 18,3 / 10/18但不适用于4/10/18 这是为了获取报告的日期范围,该报告在过去75天的所有帐户中每月运行一次,而不是在上个月报告中报告

DECLARE @TODAYS_DT DATE = '4/10/2018'

DECLARE @START_DT CHAR(10)
DECLARE @END_DT   CHAR(10)

SELECT  
    @START_DT = CONVERT(VARCHAR, DATEPART(MM, DATEADD(MM, -1, DATEADD(DAY, -75, '4/10/2018')))) + '/20/' + CONVERT(VARCHAR, DATEPART(YEAR, DATEADD(DAY, -75, '4/10/2018')))

SELECT  
    @END_DT = CONVERT(VARCHAR, DATEPART(MM, DATEADD(DAY, -75, '4/10/2018'))) + '/20/' + CONVERT(VARCHAR, DATEPART(YEAR, DATEADD(DAY, -75, '4/10/2018')))

SELECT  
    @START_DT, @END_DT

1 个答案:

答案 0 :(得分:0)

你需要进一步追溯到2017年。尝试减去100而不是75。

DECLARE @TODAYS_DT DATE = '4/10/2018'

DECLARE @START_DT CHAR(10)
DECLARE @END_DT   CHAR(10)

SELECT  
    @START_DT = CONVERT(VARCHAR, DATEPART(MM, DATEADD(MM, -1, DATEADD(DAY, -100, '4/10/2018')))) + '/21/' + CONVERT(VARCHAR, DATEPART(YEAR, DATEADD(DAY, -100, '4/10/2018')))

SELECT  
    @END_DT = CONVERT(VARCHAR, DATEPART(MM, DATEADD(DAY, -100, '4/10/2018'))) + '/20/' + CONVERT(VARCHAR, DATEPART(YEAR, DATEADD(DAY, -100, '4/10/2018')))

SELECT  
    @START_DT, @END_DT