Using a date for a datetime field in a SOQL Query [duplicate]

http://salesforce.stackexchange.com/questions/8896/using-a-date-for-a-datetime-field-in-a-soql-query



This question already has an answer here:

The SF doc explains how to put a Datetime query in a SOQL query.

SELECT Id
FROM Account
WHERE CreatedDate > 2005-10-08T01:02:03Z

I need to just put in a date for this query. Something like:

SELECT Id
FROM Account
WHERE CreatedDate > 2005-10-08

But that throws an error. Any ideas?

share improve this question

marked as duplicate by Ralph CallawayDaniel BlackhalleyescreamSaariko Feb 28 '13 at 8:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

 

3 Answers

up vote 5 down vote accepted

CreatedDate is a datetime field so I guess you would need to specify the time component.

Can you do something like where you just append the time portion to be 12 am by default.

WHERE CreatedDate > 2005-10-08T00:00:00Z

Or you can use Date Literals like

 WHERE CreatedDate > YESTERDAY

For more on date formats and more literal values see,

http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_dateformats.htm

share improve this answer
 

If you want to avoid date manipulation with Apex, you could also create a custom date formula field and query off that field. Your formula would look like this:

DATEVALUE(CreatedDate)

Then just query off your custom field.

Let me know if you have any questions.

share improve this answer
 

You'll want to use a DateTime and compute the range, or use date literals.

Date inputDate = Date.newInstance(2005,10,8);
DateTime refDate1 = DateTime.newInstance(inputDate.year(), inputDate.month(), inputDate.day(), 0, 0, 0);
DateTime refDate2 = refDate1.addDays(1);

List<Account> accounts = [
select id from Account
where createdDate >= :refDate1
and createdDate < :refDate2];

You can bind a date variable into the SOQL Query as well, but the results might not be what you expect.


This question already has an answer here:

The SF doc explains how to put a Datetime query in a SOQL query.

SELECT Id
FROM Account
WHERE CreatedDate > 2005-10-08T01:02:03Z

I need to just put in a date for this query. Something like:

SELECT Id
FROM Account
WHERE CreatedDate > 2005-10-08

But that throws an error. Any ideas?

share improve this question

marked as duplicate by Ralph CallawayDaniel BlackhalleyescreamSaariko Feb 28 '13 at 8:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

 

3 Answers

up vote 5 down vote accepted

CreatedDate is a datetime field so I guess you would need to specify the time component.

Can you do something like where you just append the time portion to be 12 am by default.

WHERE CreatedDate > 2005-10-08T00:00:00Z

Or you can use Date Literals like

 WHERE CreatedDate > YESTERDAY

For more on date formats and more literal values see,

http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_dateformats.htm

share improve this answer
 

If you want to avoid date manipulation with Apex, you could also create a custom date formula field and query off that field. Your formula would look like this:

DATEVALUE(CreatedDate)

Then just query off your custom field.

Let me know if you have any questions.

share improve this answer
 

You'll want to use a DateTime and compute the range, or use date literals.

Date inputDate = Date.newInstance(2005,10,8);
DateTime refDate1 = DateTime.newInstance(inputDate.year(), inputDate.month(), inputDate.day(), 0, 0, 0);
DateTime refDate2 = refDate1.addDays(1);

List<Account> accounts = [
select id from Account
where createdDate >= :refDate1
and createdDate < :refDate2];

You can bind a date variable into the SOQL Query as well, but the results might not be what you expect.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值