As Rudy said there are some implicit conversion going on since you are casting to a datetime and then you are comparing to what in the first comparison is a numeric value.
Unfortunately in the second when you go through an anonymous table that isn't defined the second value ends up as a string in that comparison.
So in the first case to be able to perform the comparison the datetime is implicitly cast to a integer while in the second case since x is a string the implicit cast of datetime is to string.