locked
SQL select query logic RRS feed

  • Question

  • User-254703530 posted

    Hi, 

    Here is the code i currently use, it worked fine till this month. The code is supposed to return result of 

    Month(GetDate())+3
    But I now relized it's not showing January 2013 data, I guess i need to add some kind of year checker so if current year + 3 is more than 12 then use current year + 1. But i'm not sure how i can inlucde that condition in SQL.
    SELECT * FROM [xxxx] where sent like '%U%' and Month(dateofservice) = (Month(GetDate())+3)AND Year(dateofservice) = Year(GetDate()) ORDER BY [dateofservice] DESC

    Any tip will be appriciated

    thx



    Tuesday, October 9, 2012 12:22 PM

Answers

  • User-1407477457 posted

    I suggest creating variables that represent the 1st day of 3 months into the future and the first day 4 months in the future.  Then your sql would be

    and DateOfService >= @var1

    and DateOfService < @var2

     

    This approach will probably run faster too, especially if DateOfService is indexed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 9, 2012 12:46 PM

All replies

  • User-1407477457 posted

    I suggest creating variables that represent the 1st day of 3 months into the future and the first day 4 months in the future.  Then your sql would be

    and DateOfService >= @var1

    and DateOfService < @var2

     

    This approach will probably run faster too, especially if DateOfService is indexed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 9, 2012 12:46 PM
  • User-254703530 posted

    Thanks, i solved my problem by using variables.

    Wednesday, October 10, 2012 4:12 PM