SQLTeam.com | Weblogs | Forums

Quick question on Date operations


I have seen the below code:


I would like to ask, is this really valid?

From what I know, MONTH() returns and integer so how can an integer be compared with CURRENT_TIMESTAMP which is a full date and time value?

Also, even this comparison is correct, does it really return all the values in [Date Field] where the month is less than the current month or does it return the months that are less than the current month of current year?


No, it's not valid, for the reason you mentioned. If it did return results, they would be suspect/invalid.

It is not valid - but it will not fail due to implicit conversions and precedence. The function MONTH will return an integer value - that will then be implicitly converted to a datetime - and that value would then be compared with current_timestamp.

This will return all rows - because integer datetimes 1 through 12 will be 1900-01-02 through 1900-01-13 which will always be less than current_timestamp.

1 Like