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
Wm Peck 1958Wm Peck 1958 

Can you do a CASE statement in SOQL, like Oracle?

I'm learning all the nuances of SOQL via Workbench, and as you know, it's painful for an Oracle SQL person.

I'm a data analyst, and my goal is to pull Salesforce data into Excel.

I'm just trying to do a case statement in SOQL, such as 
     case gender
          when 'F' then 'Female'
          when 'M' then 'Male'
          else 'Other'
      end

Can this be done in Workbench? I'm told you can't do it, but I need to know for sure, and what are the alternatives.

I asked one of our devleopers, and his response was a switch statement in Apex. I don't think Apex is going to be my tool, plus it's a lot more programm'y than a simple SQL CASE statement.

It looks like Einstein is the way to do this, but I haven't investigated that yet, mainly I'm trying to learn the Data Model and to just pull data. Oracle seemed a lot better in that regard.

Not sure the best topic to choose so just chose "General Development", since there's no SOQL topic.

Thank you. 
Best Answer chosen by Wm Peck 1958
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Yes,as you already know it is not possible to use CASE in the SOQL query and there is no alternative for this.

As you mentioned you can use APEX to check and then use it in the SOQL.Since you are not willing to use Apex and this can also be done by creating the formula field using the CASE on the fields and use it in the SOQL.

Reference:https://salesforce.stackexchange.com/questions/64384/using-case-in-soql-select-statement

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Yes,as you already know it is not possible to use CASE in the SOQL query and there is no alternative for this.

As you mentioned you can use APEX to check and then use it in the SOQL.Since you are not willing to use Apex and this can also be done by creating the formula field using the CASE on the fields and use it in the SOQL.

Reference:https://salesforce.stackexchange.com/questions/64384/using-case-in-soql-select-statement

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
This was selected as the best answer
Wm Peck 1958Wm Peck 1958
Good to know - - - so how do you create a formula field? If I need to do this in Apex, I'll figure it out or get someone to do it.

Thx
Wm Peck 1958Wm Peck 1958
following up - just need to know the basics on creating a formula field.

I did find this Trailhead (https://trailhead.salesforce.com/content/learn/projects/customize-a-salesforce-object/create-formula-fields), so if you think that's sufficient, just give me the thumbs up. Or additional details if appropriate.

Thanks!