Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select column, if blank select from another

How does one detect whether a field is blank (not null) and then select another field if it is?

What I really need is a IsBlank function that works the same as IsNull but with with blanks.

REPLACE doesn't work with blanks, COALESCE only works with NULLS.

like image 694
graham.reeds Avatar asked Nov 17 '09 09:11

graham.reeds


People also ask

Can if be used in select clause?

This isn't what the requester wanted, but is very useful to know that you can use if statements outside a select statement. EXISTS is good because it kicks out of the search loop if item is found. A COUNT runs until the end of table rows.

How do you return all records if parameter is null?

Inside the stored procedure, the parameter value is first tested for Null using the ISNULL function and then checked whether it is Blank (Empty). If the parameter has value then only matching records will be returned, while if the parameter is Null or Blank (Empty) then all records from the table will be returned.

Can we use select without from?

No, in Oracle there is no SELECT without FROM . Using the dual table is a good practice. dual is an in-memory table. If you don't select DUMMY from it, it uses a special access path ( FAST DUAL ) which requires no I/O .


1 Answers

How about combining COALESCE and NULLIF.

SELECT COALESCE(NULLIF(SomeColumn,''), ReplacementColumn) FROM SomeTable 
like image 160
PHeiberg Avatar answered Sep 21 '22 05:09

PHeiberg