Format Date in SOQL Query

Quick hack to format the dates for easy understanding

When writing SOQL queries, it is always tricky to work with date fields because Salesforce returns date fields in a specific format and often it is hard to understand them.

So today I am going to walk you through a quick tip using which you can render the dates in a very easy to understand format.

And that can be done with the help of format() function in SOQL queries.

Below is the full-blown SOQL query using the format() function that returns date fields in easy to understand format.

[SELECT Id, Name, FORMAT(CreatedDate) FROM Account]

It is just not that we can use format function only for date fields, we can also use this for currency fields like shown below

SELECT FORMAT(amount) Amt, format(lastModifiedDate) editDate FROM Opportunity
editDate = "7/2/2015 3:11 AM"
Amt = "AED 1,500.000000 (USD 1,000.00)"

Format function also supports aliasing. In the above query, you can see that I have used an alias for amount field as well as last modified field.

And last, am launching a new course next week — stay tuned!

Hope this is helpful.

Subscribe to Salesforce Casts to get access to more than 700+ episodes on Episodes, Hot Tips, Books, Info graphics etc.

Reply

or to participate.