Dynamic date range

  • Hi there,
    I need to write a query that displays certain data between the first day of the last month and the last day of the last month
    For example, once we are in august first i want the range to between '2017-07-01 00:00:00' and '2017-07-31:23:59:59' (or 2017-08-01 00:00:00 instead)
    and when we get to september first, the same goes with on automatically.

    This is my query...

    Select A.MemberId, M.TransactionDateTime, ...
    From Online.dbo.Media M LEFT JOIN ...
    Where  M.TransactionDateTime Between '2017-07-01 00:00:00' AND '2017-08-01 00:00:00' 

    Thanks

  • http://qa.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • JohnDoe564 - Tuesday, August 8, 2017 2:07 AM

    Hi there,
    I need to write a query that displays certain data between the first day of the last month and the last day of the last month
    For example, once we are in august first i want the range to between '2017-07-01 00:00:00' and '2017-07-31:23:59:59' (or 2017-08-01 00:00:00 instead)
    and when we get to september first, the same goes with on automatically.

    This is my query...

    Select A.MemberId, M.TransactionDateTime, ...
    From Online.dbo.Media M LEFT JOIN ...
    Where  M.TransactionDateTime Between '2017-07-01 00:00:00' AND '2017-08-01 00:00:00' 

    Thanks

    SELECT A.MemberId, M.TransactionDateTime, ...
    FROM Online.dbo.Media AS M
    LEFT JOIN ...
    WHERE M.TransactionDateTime >= DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) - 1, 0)
    AND M.TransactionDateTime < DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0)

  • DesNorton - Tuesday, August 8, 2017 2:29 AM

    JohnDoe564 - Tuesday, August 8, 2017 2:07 AM

    Thanks, it works perfectly

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply