You need to sign in to do that
Don't have an account?

Reg: SOQL Error in Dynamic SOQL with DateTime
Hi All,
I am trying to get the list of documents based on createdDate using Dynamic SOQL.
I am getting SOQL Exception in this. Please see the sample code. Any help would be greatly appreciated.
Sample Code:
Datetime fromDate = datetime.newInstance(2005,10,08, 1,02,03);
try{
string sUserQuery = 'Select Name,CreatedDate From Document where CreatedDate >' + fromDate;
system.debug('**User Query**' + sUserQuery);
List<Sobject> lstQueryRecords = Database.query(sUserQuery);
system.debug('--------lstQueryRecords-------' + lstQueryRecords);
}
catch(Exception ex){
system.debug(ex.getMessage());
}
Debug Logs : System.QueryException: line 1:76 no viable alternative at character '<EOF>'
DEBUG|line 1:76 no viable alternative at character '<EOF>'
You'll need to use the Apex variable syntax rather than just concatenating the datetime:
All Answers
You'll need to use the Apex variable syntax rather than just concatenating the datetime:
It also gives the same result:
Datetime fromDate = datetime.newInstance(2005,10,08, 1,02,03);
system.debug('**** From Date ****'+fromDate); // *2005-10-07 19:32:03
try{
List<Sobject> sUserQueryRecords=[Select Name,CreatedDate From Document where CreatedDate >: fromDate];
system.debug('--------lstQueryRecords-------' + sUserQueryRecords);
}
catch(Exception ex){
system.debug(ex.getMessage());
}
Thanks
Ankita