AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL DECODE |
|
SQL > Advanced SQL >
DECODE Function
DECODE is a function in Oracle and is used to provide if-then-else type of logic to SQL. It is not available in MySQL or SQL Server. Oracle's DECODE function maps column values to replacement values using search/result pairs — acting like an inline if-then-else. For MySQL or SQL Server, use the CASE expression to achieve the same result.
SyntaxThe syntax for DECODE is: "search_value" is the value to search for, and "result" is the value that is displayed. [ ] means that the "search_value_n", "result_n" pair can occur zero, one, or more times. ExampleWe use the following table for our example. Table Store_Information
To display 'LA' for 'Los Angeles', 'SF' for 'San Francisco', 'SD' for 'San Diego', and 'Others' for all other cities, we use the following SQL, "Area" is the name given to the column that the DECODE function operates on. Result:
To achieve what DECODE does in MySQL and SQL Server, we would use the CASE function. Frequently Asked QuestionsWhat does the SQL DECODE function do?DECODE is an Oracle function that compares a column value against a series of search values and returns the corresponding result for the first match found. If no match is found, it returns an optional default value — functioning like an inline if-then-else statement. Is DECODE available in MySQL or SQL Server?No. DECODE is Oracle-specific. In MySQL and SQL Server, use the CASE expression to achieve equivalent conditional logic. What is the syntax for Oracle DECODE?
How do you replace DECODE in MySQL or SQL Server?Use a CASE expression: |
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.