Hello friends, In today's post, I am going to describe about Format function. By reading title of the post you will thought about format function that used in apex used to format date, datetime and many more, but here I am talking about those format function which is used in SOQL and SOQL query language. So let's get started,

Format function in SOQL and SOSL

1. This function used to localized formatting to number, date and currency fields
2. As per user locale this function reflect the appropriate format of fields.
3. This function also support aliasing, aliasing is required when the query includes the same field multiple time.
4. This function used in aggregate query and also with convertCurrency() function.

Following are the syntax of this function.

In SOQL:

 SELECT FORMAT(amount)amt, format(Createddate)cDate FROM Opportunity
OR
SELECT amount,convertCurrency(amount)amt, FORMAT(convertCurrency(amount))convertamt FROM Opportunity
OR
SELECT FORMAT(MIN(closedate))cDate FROM Opportunity
In SOSL:
 FIND {Test} RETURNING Account(Id, LastModifiedDate, FORMAT(LastModifiedDate)FormattedDate)
Example:
 List<Opportunity> lstOpp=[SELECT FORMAT(amount)amt, format(Createddate)cDate FROM Opportunity WHERE Id='0060K00000Tw24r'];
String amount=(String)lstOpp[0].get('amt');
String dt=(String)lstOpp[0].get('cDate');
System.debug('amount^^ '+amount);
System.debug('Created Date^^ '+dt);

Output:

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.