function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
business Identitybusiness Identity 

Compare a CreatedDate (This is of type DateTime) and a variable MyCustomDateVariable (This is of type Date) in SOQL

Hello People,

I need to compare a CreatedDate (This is of type DateTime) and a variable MyCustomDateVariable (This is of type Date) in SOQL. I have tried with following attempts.But doesn't work:

1) DAY_ONLY(CreatedDate)=:MyCustomDateVariable

2) Date.valueOf(CreatedDate)=:MyCustomDateVariable

3) CreatedDate=:DateTime.valueOf(MyCustomDateVariable)

Thanks...
pbattissonpbattisson
You cannot compare those 2 types in SOQL directly using any of those methods, what I would suggest is (assuming you are only interested in the Date component) adding a date field on the object that you set via a default value or workflow and then compare using that. This will make your queries faster than any of the methods you have as well.
John PipkinJohn Pipkin
the DAY_ONLY function should work. It sounds like there is something else wrong with your query. Have you tried manually running the query in workbench or dev console to ensure that your query returns records?