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
KitpithKitpith 

Get Month,Year from Date

Hello,

Could anybody tell me in one field in object, Date is stored. So i need to get record showing Month for that date . Is there any method like oracle i.e. to_char(StoredDate,MM) 

 

Or any method like Month(StoredDate).

 

 

 

m.elmoussaouim.elmoussaoui

Hello,

You don't need Apex for this, you could just create a new Custom field of type Forumla and use this syntax : MONTH(date)

 

for more  : https://help.salesforce.com/htviewhelpdoc?id=customize_functions_i_z.htm&siteLang=en_US#MONTH

KitpithKitpith

Hello elmoussaoui,
I am trying To Get data In Query from Object In Apex code so don't need to work with Custom Field.

If you work with workbench Then below Is the query
SELECT RequestDate__c FROM Object_Name

Need To Get Month from that object Query

Sean TanSean Tan

If all you're trying to do is get the month / year from a Date in Apex this will work:

 

Object_Name item : [ SELECT RequestDate__c FROM Object_Name limit 1 ];

if (item.RequestDate__c != null)
{
	//Returns a numeric value representing the month (1-12)
	//1 = January
	Integer month = item.RequestDate__c.month();
	Integer year = item.RequestDate__c.year();		
}

Essentially you use the month() and year() method on the Date objects... see here for more information about the Date object in Apex:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_date.htm

Bhawani SharmaBhawani Sharma
Are you looking for something like this :
SELECT CALENDAR_MONTH(CreatedDate), SUM(Amount)
FROM Opportunity
GROUP BY CALENDAR_MONTH(CreatedDate)
ganesh thotaganesh thota
select id,CALENDAR_MONTH(createddate) from account group by CALENDAR_MONTH(createddate),id