Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
How to display default value if sql not return any row.

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.
- 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.
in below example query not return any row , so i want to display some default value.
Best 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
Answers
-
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
-
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.
-
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