Use ‘open’ Date in the Oracle SQL Where Clause

Use ‘open’ Date in the Oracle SQL Where Clause

The most common example where ‘open’ date is used is in the expiration_date for an entry. It could be set for a particular date, or be NULL if the expiration date isn’t defined (open date). It is also possible to ‘open’ date use in the where clause:

 EXP_DATE is NULL or(EXP_DATE is NOT NULL and EXP_DATE < :ar_date)

Assume that argument :ar_date could be NULL as well. Oracle doesn't allow you to use 'is Null' for arguments, but function nvl() could be used instead:

 EXP_DATE is NULL orNvl(:ar_date,' ') = ' ' or(EXP_DATE is NOT NULL and   Nvl(:ar_date,' ') <> ' ' and   EXP_DATE < :ar_date)

All the above could be combined in the single comparison expression:


First, the date's comparison is substituted by the comparison of strings. If there are any null values, in both cases they will be substituted by the minimum or maximum numbers--depending what side of comparison they are located. This is how the open date is treated. Second, the comparison operator could be '<' or '<=', which doesn't limit the technique. Plus, this method makes SQL more readable as can be seen in the example below (rewrite the example using another technique if you are not sure about this).

Usage example: If there is a need to select entries that were active in a particular date range (from :ar_from to :ar_to) and all table's values and arguments could be null, SELECT will be:

 SELECT …FROM …WHERE… andnvl(to_char(trunc(FROM_DATE),'YYYYMMDD'),'00000000')) <=nvl(to_char(trunc(:ar_from),'YYYYMMDD'),'99999999') andnvl(to_char(trunc(:ar_from),'YYYYMMDD'),'00000000')) <=nvl(to_char(trunc(TO_DATE),'YYYYMMDD'),'99999999') andnvl(to_char(trunc(FROM_DATE),'YYYYMMDD'),'00000000')) <=nvl(to_char(trunc(:ar_to),'YYYYMMDD'),'99999999') andnvl(to_char(trunc(:ar_to),'YYYYMMDD'),'00000000')) <=nvl(to_char(trunc(TO_DATE),'YYYYMMDD'),'99999999');
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