locked
Where clause RRS feed

  • Question

  • Declare @date date = '2015-02-12'

    Column in table datetime format

    when i  use  where clause

    WHERE @Date > ValidateDateTime ,  got no rows  but when i changed ValidateDateTime > @date, getting result as expected,

    this behavior because of data format or  something else

    Thanks

    Tuesday, August 11, 2015 1:58 PM

Answers

  • Guess you're running a select statement on the lines of..

    Declare @date date = '2015-02-12'

    Select * from table1 where @Date > ValidateDateTime

    and then you ran

    Declare @date date = '2015-02-12'

    Select * from table1 where ValidateDateTime >@Date

    It is perfectly simple; your where clause was incorrect as you were attempting to filter on your variable being greater than your field data rather than your field data being greater than your variable value; therefore the second method is correct.


    Please click "Mark As Answer" if my post helped. Tony C.


    • Proposed as answer by Tom Phillips Tuesday, August 11, 2015 2:08 PM
    • Marked as answer by Vaishu Tuesday, August 11, 2015 2:26 PM
    • Edited by Anthony C-UK Tuesday, August 11, 2015 2:59 PM spelling mistake
    Tuesday, August 11, 2015 2:07 PM
  • Surely because ValidateDateTime is greater than @date. That's why you have no results.

    To be sure about that, print ValidateDateTime to see.


    Please click "Mark As Answer" if my post helped.

    • Marked as answer by Vaishu Tuesday, August 11, 2015 2:27 PM
    Tuesday, August 11, 2015 2:08 PM

All replies

  • Guess you're running a select statement on the lines of..

    Declare @date date = '2015-02-12'

    Select * from table1 where @Date > ValidateDateTime

    and then you ran

    Declare @date date = '2015-02-12'

    Select * from table1 where ValidateDateTime >@Date

    It is perfectly simple; your where clause was incorrect as you were attempting to filter on your variable being greater than your field data rather than your field data being greater than your variable value; therefore the second method is correct.


    Please click "Mark As Answer" if my post helped. Tony C.


    • Proposed as answer by Tom Phillips Tuesday, August 11, 2015 2:08 PM
    • Marked as answer by Vaishu Tuesday, August 11, 2015 2:26 PM
    • Edited by Anthony C-UK Tuesday, August 11, 2015 2:59 PM spelling mistake
    Tuesday, August 11, 2015 2:07 PM
  • Surely because ValidateDateTime is greater than @date. That's why you have no results.

    To be sure about that, print ValidateDateTime to see.


    Please click "Mark As Answer" if my post helped.

    • Marked as answer by Vaishu Tuesday, August 11, 2015 2:27 PM
    Tuesday, August 11, 2015 2:08 PM