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
OnDem DevOnDem Dev 

Need help on Dynamic SOQL query with date condition in where clause

Hi,

 

I constructed a dynamic soql query and query looks like below,

 

Date dateVal = Date.today();

Database.query('select field1, field2, field3, dateField1 from Object__c where field1 = :val1 && dateField1 >= :dateVal');

As binding variables is supported, the formed query should work.

 

But i am getting no result when i sun this query.

Can any one help me out with a possible solution?

 

Any help on this is highly appreciated.

 

Thanks,

OnDemand

SuperfellSuperfell
You can't do binding with dynamic SOQL, you'll need to format and inline the date values yourself.
OnDem DevOnDem Dev

I tried even that format, but id doen'nt work out. This is what i did,

 

Database.query('select field1, field2, field3, dateField1 from Object__c where field1 = '\'+val1+\'' and  dateField1 >= '\'+dateVal+\'' ');

 

Or can you just correct me where i am wrong.

Or can you provide with an example

 

Thanks,

OnDemand

SuperfellSuperfell
You need to format the date values into the correct string format, see the examples in the SOQL docs section of the API docs.
SalesforceSSOSalesforceSSO

Hi,

 

I was trying to convert a servertimestamp to date to add it to query. I am reading servertimestamp from a file as a string.

 

SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'");

 

String date = simpleDate.format("Tue Apr 07 10:54:46 EDT 2009");

 

System.out.println(date);

 

This is giving exception

 

any suggestions.