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
ajaybharathajaybharath 

how to query using month value of date in sfdc

Hi

 

I'm trying to query for month value from a date field f my custom object
like say ,
SELECT Id,Date__c from custom_obj__c where . . . . .
using date__c.getMonth() or MONTH(date__c). i get an error that "Invalid aggregate Function"

I dont want to create a formula field and query based on it . Is ter any possibility without creating formula field for date .
Thanks in advance

ajay

Best Answer chosen by Admin (Salesforce Developers) 
Shivanath DevnarayananShivanath Devnarayanan

I think there is a little misunderstanding here, I'd like to step in and clarify if possible please correct me if I'm missing something

 

You have Picklists for month and Days and a go button in your visual force  this is for selection or filtering purposes only;

and once you hit Go you want to get a list of particular SObjects that have date sections "month and Year" mentioned in the VF pick list; inorder to do that you need to split out the Date field on the custom object .

 

This is just a pseudocode do make the required correction as you may seem fit

 

Select Id, Date__c from Object where CALENDAR_MONTH(Date__c) == MonthInVF

 you can check out the list of functions available for SOQL here

 

Let me know if it helps !

 

/* If this has solved your problem,do mark this as answered so others may find it helpful */

 

 

All Answers

sourav046sourav046

I dont think its possible in SOQL .

 

The best place for using these functions are formula fields and workflow rules .

ajaybharathajaybharath

Thanks for your assistance sourav

Shivanath DevnarayananShivanath Devnarayanan

Hey Ajay,

 

If you could better explain the scenario you're trying to use this maybe we could suggest a better approach.

but if the previous post helped you solve your issue mark it as answered so others may benefit from it ..

 

happy coding !

ajaybharathajaybharath

Hi Shiv

Thanks for ur assistance

this is my requirement , i have 2 picklist values like month with values Jan - Dec and year with values 2009 - 2012 and i have a go button . I have a custom object and a field in it is date. i have to render records based on the month and year selected from picklist in a pageblocktable. I have been asked not to include additional fields in the custom object. so I was trying to query for the month value of a date field without using a formula field to retrieve

I'm still stuck with it .


Regards
ajay

sourav046sourav046

I guess the ISPICKVAL() function could help you .

 

Try this link to get help on this function .

ajaybharathajaybharath

Hi Sourav


I think ISPICKVAL() function involves formula field .. using formula field i can do this MONTH(Date__c) and YEAR(Date__c) and get the job done . But my requirement is not using formula field i have to query for month value of date field in a apex class.

Shivanath DevnarayananShivanath Devnarayanan

Hello again Ajay,

 

Correct me if I'm wrong, you are trying to use apex class / controller to get the month of a date field 

 

to do that you can use the inbuilt date methods like month. here is the full methods that can be used in APEX which is the programatic approach

 

Declarativly I'm not sure how you can come to the solution, I may be missing something, but nevertheless here is a list of advanced formulas that may come in handy if you ever need them. and also some SOQL date Literals

 

Do let me know if we can help you out more

 

/* If this has solved your problem,do mark this as answered so others may find it helpful */

ajaybharathajaybharath

Hi Shiv

Thanks for your efforts and replies. I think You have understood the requirement. I searched all your links you have provided .
I just need the exact method to get the month value of date field. Can you help me out with this statement
SELECT Id,Date__c from custom_obj__c where ******
in this above statement ,
in place of *****
i tried this Date__c.getMonth() -> this resulted in error
secondly , i tried this MONTH(Date__c) -> Invalid Function MONTH error

Can you suggest a method to retreive month from date here

 

regards

Ajay

sourav046sourav046

Got your requirement now .

 

I dont understand why are you going to retrieve Month and Year from Date as you have those as individual objects .

 

Try this :

SELECT Id,Date__c from custom_obj__c where Month__c='Oct' and Year__c=2009

 I guess you must have got the logic .Change the String values as given in your Picklist .

 

\*If this answer resolves your query, mark it as resolved*\

 

 

ajaybharathajaybharath

No dude there is no field as Month__c or Year__c .. say for example if i have DATE as 4/10/2012 .. this format follows MM/DD/YYYY . From this date i have to get month and year either as integer or string. so i'm asking if anyone could suggest there is any method for getting this in a query in the where clause?

Ajay

sourav046sourav046

But previously you said ,"i have 2 picklist values like month with values Jan - Dec and year with values 2009 - 2012 and i have a go button"

 

So what did that mean ?

Shivanath DevnarayananShivanath Devnarayanan

I think there is a little misunderstanding here, I'd like to step in and clarify if possible please correct me if I'm missing something

 

You have Picklists for month and Days and a go button in your visual force  this is for selection or filtering purposes only;

and once you hit Go you want to get a list of particular SObjects that have date sections "month and Year" mentioned in the VF pick list; inorder to do that you need to split out the Date field on the custom object .

 

This is just a pseudocode do make the required correction as you may seem fit

 

Select Id, Date__c from Object where CALENDAR_MONTH(Date__c) == MonthInVF

 you can check out the list of functions available for SOQL here

 

Let me know if it helps !

 

/* If this has solved your problem,do mark this as answered so others may find it helpful */

 

 

This was selected as the best answer
ajaybharathajaybharath

Its the VF i created that has 2 picklist values with month and year to filter those from the Date__c field that i have in custom_Obj__c

ajaybharathajaybharath

Hi Shiv

Thanks a ton

That worked perfectly , i was searching for this CALENDAR_MONTH method

a little thing to add to ur reply is , this function returns an Integer and i had to convert my picklist values to fetch monthinvf as Integer in class.

 

Thanks & Regards

Ajay