Skip to content
Advertisement

Conditional Date Comparison and Assignment using Case expression in Oracle SQL

I am new to the site and SQL. I need some help with a case expression.

The requirement is as follows:

I have a table T1 with two date columns – eff and disc;

A second table T2 with 2 date columns – on_date & off_date;

I am trying to build a single case expression where i can compare the date columns btw both the tables and assign values based on certain conditions.

The Conditions are:

• If T1.eff and T2.disc dates are set to default , i.e. T1.eff=1/1/1970 and T2.disc=1970

• If T1.eff >T2.on_date and T1.disc >T2.off_date

• If T1.eff

• If T1.eff T2.off_date

• If T1.eff >T2.on_date and T1.disc

I started writing a Case expression and i am stuck on how to build/write the block; where i need to compare both ‘eff’ and ‘disc’ dates as one condition and then assign the respective value to both ‘eff’ and ‘disc’ in a single case expression.

ETC.

I am not sure if we can get/use two columns in a single case expression.

Oracle DB – Client version: 12.1.0.2.0

Thanks in Advance! => VPPG

Advertisement

Answer

I think what you want is to modify the values for EFF and DISC that are displayed in the SELECT output. You can’t easily do that with a single case statement, since you want to change the output of 2 columns. But if you simplify your logic, you can still keep it pretty simple.

Also note that I’m using a different form of the CASE expression – there are two ways to use it.

If this doesn’t really solve your problem, let me know and I’ll update my answer.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement