Hi @kkran
The posting date is in the int format.
You said that the datatype of PostingDate is INT . And as Tom said, SQL Server does not add commas to numbers.Therefore you don't need to use REPLACE function.
And I don't want to calculate going back only 425 days. Because it should calculate automatically
Do you mean :
when excute query today ,the date range is between 'DateA' and 'DateA+270days' ;
when excute on tomorrow, the date range is between 'DateA+1days' and 'DateA+271days';
when excute the day after tomorrow, the date range is between 'DateA+2days' and 'DateA+272days',and so on.
If I am right,then check this query:
SELECT * FROM YourTable
WHERE LTRIM(PostingDate) BETWEEN DATEADD(DAY, -DATEDIFF(D,'DateA','Today'), CONVERT(DATE, GETDATE()))
AND DATEADD(DAY, 270, DATEADD(DAY, -DATEDIFF(D,'DateA','Today'), CONVERT(DATE, GETDATE())));
What you need to do is to modify the two dates 'DateA' and 'Today' only once ,and then it will calculate automatically tomorrow,the day after tomorrow ......
If I assume 'Today' is March 4th,2022 and DATE A is 'Jan 1, 2021',the code will be like:
SELECT * FROM YourTable
WHERE LTRIM(PostingDate) BETWEEN DATEADD(DAY, -DATEDIFF(D,'20210101','20220304'), CONVERT(DATE, GETDATE()))
AND DATEADD(DAY, 270, DATEADD(DAY, -DATEDIFF(D,'20210101','20220304'), CONVERT(DATE, GETDATE())));
Best regards,
LiHong