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
billkatbillkat 

Forming a couple of SOQL date queries

Hi all

 

Excuse the basic question... I'm struggling with forming two simple queries, of these types:

 

Select x from y WHERE LastmodifiedDate = 2010-05-17

 

Select x from y WHERE LastmodifiedDate > 2010-05-10 AND LastmodifiedDate < 2010-05-19

 

This is to run the Data Loader in batch mode, if it matters.

Tried to muddle through the API docs - failed to find anything that clarified this.

cheers

Bill

 

Best Answer chosen by Admin (Salesforce Developers) 
Mark SFMark SF

Try using the date functions. For example:

 

SELECT Id FROM Lead WHERE DAY_ONLY(LastModifiedDate) = 2010-05-03

 

See  Date Functions doc for variations and other functions. Look at convertTimezone() too if you want results for a user's time zone as opposed to UTC.

All Answers

imuino2imuino2

I think the problem is the query needs a datetime value, and thats a string with only date.

You can achieve it in two ways, doing this

 

myDate = Date.valueOf('2010-05-17');

Select x from y WHERE LastmodifiedDate = myDate;

 

or simply by giving your string the datetime format which is

yyyy-mm-ddThh:mm:ss.mseZ

in this case

Select x from y WHERE LastmodifiedDate = 2010-05-17T00:00:00.000Z.

 

Ignacio.

billkatbillkat

Thanks Ignacio

 

The first looks interesting, will try it.

 

The second matches on the specific time part as well, so doesn't get any records. i.e. if a record is modified at 15pm the timepart needs to be 15:00:00, kind of thing.

 

To get rcds on a single past day I've now got this which works but is really ugly:

WHERE (LastmodifiedDate > 2010-05-17T00:00:00Z) AND (LastmodifiedDate < 2010-05-17T23:59:59Z)

 

Mark SFMark SF

Try using the date functions. For example:

 

SELECT Id FROM Lead WHERE DAY_ONLY(LastModifiedDate) = 2010-05-03

 

See  Date Functions doc for variations and other functions. Look at convertTimezone() too if you want results for a user's time zone as opposed to UTC.

This was selected as the best answer
billkatbillkat

Excellent, many thanks Mark...