Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to display default value if sql not return any row.

Vemula MuniSep 13 2019 — edited Sep 20 2019

Hi All,

Below is my query. In that i will pass the emplid and comparison date dynamically.

For that emplid and date combination, if project id is present then i need to fetch that project id. if project id is not present then i need to display some default value.

  1. When query returns project id i want to exclude the default value, if query not return any row then i need to get some default value.

Note : Query may return one or more projectid for one employee.

SELECT B.PROJECT_ID FROM PS_CTSRM_ASGNSR_VW B , PS_RS_ASSGN_DETAIL C WHERE B.ASSIGNMENT_ID = C.ASSIGNMENT_ID AND

B.EMPLID = '155114'

AND B.ASSIGN_STS IN ('A','C') AND TO_DATE('2019-08-01','YYYY-MM-DD') BETWEEN C.START_DT AND C.END_DT

In below example my query returns value,so no need the default value in this case.

pastedImage_0.png

in below example query not return any row , so i want to display some default value.

pastedImage_1.png

This post has been answered by L. Fernigrini on Sep 13 2019
Jump to Answer

Comments

L. Fernigrini
Answer

Please, this is not the first question you ask, follow the guidelines!!!

It is imposible to reproduce what you show in the pictures, please copy and paste CODE!!

You should consider something like:

WITH vDesiredData as (SELECT xxx, yyy, zzz FROM YourTable WHERE .....),

     vDefaultData as (SELECT  'A', 1, SYSDATE FROM DUAL)

SELECT * FROM vDesiredData

UNION ALL

SELECT * FROM vDefaultData WHERE NOT EXISTS (SELECT 1 FROM vDesiredData);

In this case if there is data that can be returned, the UNION won't add anything since the NOT EXISTS in the WHERE clause will evaluate to false.

On the other case, when there is no data on vDesiredData, the first SELECT FROM vDesiredData wont return anything but the UNION ALL will incude whatever you configured in vDefaultData

Marked as Answer by Vemula Muni · Sep 27 2020
BrunoVroman

Hello Vemula,

what default value? And maybe it depends of the reason for "no match" (for example: no such emplid but assignemnt_id and assign_sts and start_dt end_dt OK for at least 1 other emplid?... )

Or simply:

WITH your_query AS
( SELECT b.project_id, ... FROM ... WHERE ... )
SELECT NVL( b.project_id, 'your_default_value' ) project_id
  FROM dual
  LEFT OUTER JOIN your_query ON 1 = 1
;

(maybe you want something like "No such combination!" as default value?

Best regards,

Bruno Vroman.

jaramill

Yes I agree with @"L. Fernigrini" this is NOT your first post. Please follow the guidelines and also....don't leave threads unmarked answered! You have 3 of them recently where you just disappear

pastedImage_0.png

1 - 3

Post Details

Added on Sep 13 2019
3 comments
8,674 views