这可能是一件容易的事。我在从日期字符串中获取正确的日期格式以在动态 SOQL 语句的 where 子句中使用时遇到问题。我尝试了日期函数的变体,但仍然无法找出正确的格式。自定义对象字段的类型为 Date。感谢所有建议,谢谢。
public string startDate {get;set;}
public string endDate {get;set;}
public void mymethod(){
string SOQLString = 'SELECT Id, Name FROM Time__c WHERE Closed__c = true';
if(startDate != null && endDate != null && startDate.length() > 0 && endDate.length() > 0)
{
Date sd = Date.Parse(startDate);
Date ed = Date.Parse(endDate);
SOQLString = SOQLString + ' and Date_Incurred__c >= ' + sd + ' and Date_Incurred__c <= ' + ed;
//SOQLString = SOQLString + ' and Date_Incurred__c >= \'' + sdt + '\' and Date_Incurred__c <= \'' + edt + '\'';
}
List<Time__c> times = Database.Query(SOQLString);
...
SOQLString 的输出
SELECT Id, Name FROM Time__c WHERE Closed__c = true and Date_Incurred__c >= 2013-09-27 00:00:00 and Date_Incurred__c <= 2013-09-02 00:00:00
错误
System.QueryException: line 1:1082 no viable alternative at character ' '