Use Decode Function for Oracle Queries

Use Decode Function for Oracle Queries

Decode is a very useful and handy function for Oracle queries. Itreplaces the complex If-Then-Else logic, which is used to display different things based on different values in a column.

Consider the following example in which the Viewable column can have values of 1,2, and 3:

 SELECT FirstName, ViewableFROM   employee;Results:FIRSTNAME		Viewable===========================John			1Tim			2Julie			2Stacy			1Rahul			3Leena			4Amy			1Bill			3Teri			3

Now, we can use Decode to display different things in a report based on the values inViewable.

 SELECT Firstname,      Decode(Viewable, 1,'VISIBLE', 2,'INVISIBLE', 
3,'UNKNOWN', 'OTHER')FROM employee;Results:FIRSTNAME Viewable===========================John VISIBLETim INVISIBLEJulie INVISIBLEStacy VISIBLERahul UNKNOWNLeena OTHERAmy VISIBLEBill UNKNOWNTeri UNKNOWN

Decode checks the column values and interprets the provided valuesin pairs. This is how it works:

 Switch viewable:Case 1:    Result = VISIBLECase 2:    Result = INVISIBLECase 3:    Result = UNKNOWNDefault:    Result = OTHEREnd CaseOR====================If Viewable = 1 Then    Result = VISIBLEElsif Viewable = 2 Then    Result = INVISIBLEElsif Viewable = 3 Then    Result = UNKNOWNElse    Result = OTHEREnd If

One can use Decode in Updates as well. For example, instead of writing 50 different updates to change state names, we can do it in one Update using Decode as follows:

 Update employeeset    homestate = decode(homestate,                         'AL', 'Alabama',                        'AK', 'Alaska',                        'AS', 'American Samoa',                        'AZ', 'Arizona',				. . . . . . . .                        . . . . . . . .                         'WA', 'Washington',                        'WV', 'West Virginia',                        'WI', 'Wisconsin',                        'WY', 'Wyoming' , homestate) where homecountry = 'UNITED STATES'

This will replace state abbreviations with state names. And it leaves theabbreviations alone in case it doesn’t match any of the values, such as AL, AK, etc.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as