Set default value in select statement(not use UNION statement)
Solution 1
SELECT ifnull(a,20) FROM TBL_TEST
Selects 20 if a is null otherwise selects a (in mysql, not sure about others)
Solution 2
For a portable solution, how about:
select coalesce(a, 0)
from TBL_TEST
right outer join DUAL on null is null
The COALESCE function is used here because it is more portable than NVL() or IFNULL().
You would have a DUAL table created in database systems that use a different name, such as SQL Server or DB2.
Solution 3
In Oracle:
select nvl(a, 0)
from DUAL left join TBL_TEST on null is null
Solution 4
MySQL has the DEFAULT
function, but I'm not sure how standard or widely supported it is.
Solution 5
use the COALESCE() to convert the null value column with its default value such as
select coalesce(a,0) from TBL_TEST
Rong Nguyen
My favorite technologies: ERP: Apache OFBiz(Sales, Purchasing, Accounting, HRM, Inventory, POS, CRM) Telco Application: core network, roaming Java Database: Oracle, Postgresql JSF/Primefaces Docker, Apache Mesos, Flocker, K8S Integration, OSGi and Messaging: Apache Camel, Apache Karaf and Apache ActiveMq GNU/Linux Git My profile: http://careers.stackoverflow.com/rongnk Now, I am a freelancer, feel free to contact me if you want :) Email: [email protected] Website: https://iamsoftware.com.vn
Updated on July 01, 2022Comments
-
Rong Nguyen almost 2 years
I would like to have a SELECT statement that will return specified default values if no rows are returned from the database.
We can use UNION to get the desired result like this question: "How to set a default row for a query that returns no rows?", but this gives an extra result row.
example:
SELECT a from TBL_TEST UNION SELECT 0 FROM DUAL
Is there a better way, or a standard SQL way to do this that will be portable across multiple database engines?
-
John Woo about 11 yearswhat RDBMS really? all of them?
-
Rong Nguyen about 11 yearsyes, i use mysql, oracle and sql server :)
-
Ed Plese about 11 yearsThis question looks similar: stackoverflow.com/questions/285666/…
-
Rong Nguyen about 11 yearsThe solution is still use UNION, so when i have many column in select statement, i think it make poor performance :)
-
-
Rong Nguyen about 11 yearsReturns the default value for a table column ? I need default value i put in sql, not in table structure :)
-
Rong Nguyen about 11 yearsThank, it is similar to ISNULL in SqlServer, NVL in Oracle, but not true :)
-
Rong Nguyen about 11 yearsThank, it can use for NULL value, but can not for empty value.
-
Rong Nguyen about 11 yearsThank, but it can use for NULL value, but can not for empty value
-
MGPJ about 11 yearsI have checked my query and it returns the empty value also in mysql
-
tadman about 11 yearsMySQL also has the simple
IFNULL()
method to convertNULL
values to whatever. -
Rong Nguyen about 11 yearsYou create a table with no data, then check above sql, please :)
-
Anvesh about 11 yearsIf there is empty string and first convert empty string into null using NULLIF function ..... for detail see my second answer on this question...... thanks.
-
Rong Nguyen about 11 yearsok, you try to create a table with no data, and then use above sql, please :)
-
Anvesh about 11 yearsWell, if there is no data means table is empty in case of this database engine can't perform any function on table. Function and logic will work on Data. For empty string and null you can see my new answer thanks.... :)
-
Anvesh about 11 yearsohhh. How can this possible if you find anything then please let me know :) . One more thing you can define any default value mean default constraint in Oracle, Sql Server , any . So when you send null value on that default constraint field at that time It will automatically insert default value for that null. let me know for further query.
-
Rong Nguyen about 11 yearsYes, but i have three query with three default value on the same column, so i can not use default column value :)
-
Rong Nguyen about 11 yearsSELECT NVL(a,0) FROM DUAL, are you sure ?
-
Anvesh about 11 yearsso now here it's require operation on data rather than setting default value for that there should be data . thanks :)
-
Rong Nguyen about 11 yearsThat working fine in oracle, but i am suspecting the performance compare to use UNION :-?
-
WarrenT about 11 yearsThis does not solve the problem when there are no rows returned from the table.
-
WarrenT about 11 yearsThe solution being sought will provide default values when no rows are returned from the table.
-
WarrenT about 11 yearsThe solution being sought will provide default values when no rows are returned from the table.
-
Rong Nguyen about 11 yearsthank for your attention, so in each database system i have to use or create table like DUAL table(oracle) for each query :)
-
WarrenT about 11 yearsOr you could substitute the name of the equivalent table for the particular RDBMS. For example in DB2 for i, we would use SYSIBM.SYSDUMMY1.
-
WarrenT about 11 yearsActually, for DB2 environments, you might use
CREATE ALIAS DUAL FOR SYSIBM.SYSDUMMY1