Using the IP Address in the Select Statement

Using the IP Address in the Select Statement

IP addresses logically are four numbers. While they are stored in the DB, they are usually held in only one field. I have encountered two types of such storage:

1) VARCHAR(15)’; /7#151; four decimal numbers with a (.) separator concatenated to a string.

2) CHAR(8)?four hexadecimal numbers concatenated to a string without any separator.

For validation purposes, direct displaying the column in GUI is not recommended. The IP value should be broken into four parts. Each of these should be validated separately as a whole number (the first number couldn’t be zero). For instance, if the hexadecimal format is used for storing the IP address, the SELECT statement that returns four partial numbers will be:

 SELECTto_number(DECODE(substr("TABLE"."IP_ADDR",1,1),'a','10','b',_'11','c','12','d','13','e','14','f','15',_		substr("TABLE"."IP_ADDR",1,1))) * 16 +to_number(DECODE(substr("TABLE"."IP_ADDR",2,1),'a','10','b',_'11','c','12','d','13','e','14','f','15',	substr("TABLE"."IP_ADDR",2,1))) A1,to_number(DECODE(substr("TABLE"."IP_ADDR",3,1),'a','10','b',_'11','c','12','d','13','e','14','f','15',	substr("TABLE"."IP_ADDR",3,1))) * 16 +to_number(DECODE(substr("TABLE"."IP_ADDR",4,1),'a','10','b',_'11','c','12','d','13','e','14','f','15',	substr("TABLE"."IP_ADDR",4,1))) A2,to_number(DECODE(substr("TABLE"."IP_ADDR",5,1),'a','10','b',_'11','c','12','d','13','e','14','f','15',	_substr("TABLE"."IP_ADDR",5,1))) * 16 +to_number(DECODE(substr("TABLE"."IP_ADDR",6,1),'a','10','b',_'11','c','12','d','13','e','14','f','15',	substr("TABLE"."IP_ADDR",6,1))) A3,to_number(DECODE(substr("TABLE"."IP_ADDR",7,1),'a','10','b',_'11','c','12','d','13','e','14','f','15',	_substr("TABLE"."IP_ADDR",7,1))) * 16 +to_number(DECODE(substr("TABLE"."IP_ADDR",8,1),'a','10','b',_'11','c','12','d','13','e','14','f','15',	_substr("TABLE"."IP_ADDR",8,1))) A4    FROM "TABLE";

For update purposes, use the following statement:
UPDATE “TABLE” SET “IP_ADDR” =DECODE(to_char(Floor(A1/16,0),’FM0′),’10’,’a’,’11’,’b’,’12’,’c’,’13’,’d’,’14’,’e’,’15’,’f’,to_char(Floor(A1/16,0), ‘FM0′)) ||DECODE(to_char(Mod(A1/16),’FM0′),’10’,’a’,’11’,’b’,’12’,’c’,’13’,’d’,’14’,’e’,’15’,’f’, to_char(Mod(A1/16), ‘FM0′)) ||DECODE(to_char(Floor(A2/16,0),’FM0′),’10’,’a’,’11’,’b’,’12’,’c’,’13’,’d’,’14’,’e’,’15’,’f’,to_char(Floor(A2/16,0), ‘FM0′)) ||DECODE(to_char(Mod(A2/16),’FM0′),’10’,’a’,’11’,’b’,’12’,’c’,’13’,’d’,’14’,’e’,’15’,’f’, to_char(Mod(A2/16), ‘FM0′)) ||DECODE(to_char(Floor(A3/16,0),’FM0′),’10’,’a’,’11’,’b’,’12’,’c’,’13’,’d’,’14’,’e’,’15’,’f’,to_char(Floor(A3/16,0), ‘FM0′)) ||DECODE(to_char(Mod(A3/16),’FM0′),’10’,’a’,’11’,’b’,’12’,’c’,’13’,’d’,’14’,’e’,’15’,’f’, to_char(Mod(A3/16), ‘FM0′)) ||DECODE(to_char(Floor(A4/16,0),’FM0′),’10’,’a’,’11’,’b’,’12’,’c’,’13’,’d’,’14’,’e’,’15’,’f’,to_char(Floor(A4/16,0), ‘FM0′)) ||DECODE(to_char(Mod(A4/16),’FM0′),’10’,’a’,’11’,’b’,’12’,’c’,’13’,’d’,’14’,’e’,’15’,’f’, to_char(Mod(A4/16), ‘FM0’))This technique comes from the idea of using Decode function for queries (“Use Decode Function for Oracle Queries” by Trupti Rajparia from Phoenix, Arizona). The proposed idea allows for the separation of the DB query and GUI validation.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

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