I would like to write a select where I show the value of the field as normal except when the field is null. If it is null I'd like to show a special text, for example "Field is null". How would I best do this?
// Oracle newbie
I like to use function COALESCE for this purpose. It returns the first non-null value from given arguments (so you can test more than one field at a time).
SELECT COALESCE(NULL, 'Special text') FROM DUAL
So this would also work:
SELECT COALESCE(
First_Nullable_Field,
Second_Nullable_Field,
Third_Nullable_Field,
'All fields are NULL'
) FROM YourTable
Just insert the NVL PL/SQL function into your query
SELECT NVL(SOMENULLABLEFIELD,'Field Is Null') SOMENULLABLEFIELD
FROM MYTABLE;
More detail here : http://www.techonthenet.com/oracle/functions/nvl.php
You could also use DECODE:
select value, decode(value, NULL, 'SPECIAL', value) from
(select NULL value from dual
union all
select 2 value from dual
)
Related
I am currently using the pg_column_size to calculate the size of a row in the DB.
something like:
select pg_column_size(col1, col2, col3..... col10) from _table
Problem is when 1+ column value is null the whole function returns null.
Is there anyway to set a default value for each column within the function to avoid getting null?
try
select pg_column_size(<table_name>.*) from <table_name>;
Are you specifically trying to select a subset of columns?
If you do select pg_column_size(table) from table;, it should work.
I'm dealing with a system that accepts data loads in XML format. For example, there's a field called "col1", and that field has the value "world" in it. The system interprets <col1 />, <col1></col1>, and a missing <col1> element as "no change" to the field called col1. (This is good because, if we were creating new data, "no change" would mean to accept whatever the default value is.) If I need to delete whatever is in the field, the <col1> element needs to have an xsi:nil attribute with a value of true.
So, when I'm extracting data from one instance of the system to load into another instance (inserting with SQL is not an option), I need to conditionally add xsi:nil="true" attribute to the XML returned from a query in Oracle 12c to explicitly indicate that the value of the element is null. (Always adding xsi:nil with a value of true or false, as appropriate, could work but is not desirable as it breaks convention and bloats file size.)
A test case can be set up as follows.
create table table1 (id number(10), col1 varchar2(5));
insert into table1 values (1,'hello');
insert into table1 values (2,null);
commit;
I want to to get this back from a query:
<outer><ID>1</ID><COL1>hello</COL1></outer>
<outer><ID>2</ID><COL1 xsi:nil="true"></COL1></outer>
This query throws an error.
select
xmlelement("outer",
xmlforest(id),
(case col1
when null then xmlelement(COL1, xmlattributes('xsi:nil="true"'), null)
else xmlforest(col1)
end)
)
from table1
;
Is there some other way to conditionally include the xmlattributes call, or some other way to get the output I want?
You can use NVL2 to make it slightly less verbose:
Query 1:
SELECT XMLELEMENT(
"outer",
XMLFOREST( id ),
XMLELEMENT( col1, xmlattributes( NVL2(col1,NULL,'true') as "xsi:nil"), col1 )
).getClobVal() AS element
FROM table1;
Result:
OUTPUT
-----------------------------------------------------
<outer><ID>1</ID><COL1>hello</COL1></outer>
<outer><ID>2</ID><COL1 xsi:nil="true"></COL1></outer>
Query 2: You could also use XMLFOREST to generate the elements and then APPENDCHILDXML to append the missing element (including namespaces are left as an exercise to the OP):
SELECT APPENDCHILDXML(
XMLELEMENT( "outer", XMLFOREST( id, col1 ) ),
'/outer',
NVL2( col1, NULL, XMLTYPE('<COL1 nil="true"></COL1>') )
).getClobVal() AS element
FROM table1;
Result:
OUTPUT
-------------------------------------------
<outer><ID>1</ID><COL1>hello</COL1></outer>
<outer><ID>2</ID><COL1 nil="true"/></outer>
I found that this query works, but it's more verbose than I would like.
select
xmlelement("outer",
xmlforest(id),
xmlelement(col1,xmlattributes(case when col1 is null then 'true' else null end as "xsi:nil"), col1)
).getClobVal()
from table1
;
I have a table with a massive number of columns. So many, that when I do SELECT * I can't even see any values because all the columns fill up the screen. I'd like to do something like this:
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND <THIS COLUMN> IS NOT NULL
Is this possible? Note: VALUE is not a column.
There are so many questions on SO that ask this same question, but they have some bizarre twist, and the actual question is not answered.
I've tried:
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND VALUE NOT NULL
*
Invalid relational operator
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND VALUE <> ''
*
'VALUE': invalid identifier
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND COLUMN NOT NULL
*
Missing Expression
Bonus Questions:
Is there any way to force Oracle to only show one output screen at a time?
Is there a variable to use in the WHERE clause that relates to the current column? Such as: WHERE this.column = '1', where it would check each column to match that expression?
Is there any way to get back your last command in Oracle? (I have to remote into a Linux box running Oracle - it's all command line - can't even copy/paste, so I have to type every command by hand, with a wonky connection, so it's taking an extremely long time to debug this stuff)
If you are trying to find all the non null column values for a particular record you could try an unpivot provided all the columns you are unpivoting have the same data type:
SELECT *
FROM (select * from my_table where name like '%unique value%')
UNPIVOT [include nulls] (col_value FOR col_name IN (col1, col2, ..., coln))
with the above code null values will be excluded unless you include the optional include nulls statement, also you will need to explicitly list each column you want unpivoted.
If they don't all have the same data type, you can use a variation that doesn't necessarily prune away all the null values:
select *
from (select * from my_table where name like '%unique value%')
unpivot ((str_val, num_val, date_val)
for col_name in ((cola, col1, date1)
,(colb, col2, date2)
,(colc, col3, date1)));
You can have a fairly large set of column groups, though here I'm showing just three, one for each major data type, with the IN list you need to have a column listed for each column in your column group, though you can reuse columns as shown by the date_val column where I've used date1 twice. As an alternative to reusing an existing column, you could use a dummy column with a null value:
select *
from (select t1.*, null dummy from my_table t1 where name like '%unique value%')
unpivot ((str_val, num_val, date_val)
for col_name in ((dummy, col1, date1)
,(colb, dummy, date2)
,(colc, col3, dummy)));
Have tried this?
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND value IS NOT NULL;
Oracle / PLSQL: IS NOT NULL Condition
For row number:
SELECT field1, field2, ROW_NUMBER() OVER (PARTITION BY unique_field) R WHERE R=1;
Usually in Linux consoles you can use arrow up&down to repeat the last sentence.
I'm trying to understand how Oracle handles NULL values. This is really more of a request for confirmation of what I've already discovered, to make sure I haven't overlooked something obvious. Below are scripts to confirm that Oracle handles NULL values the same as it handles a blank string. If such is the case, then pray tell why would any Oracle database be developed to ever allow a field to be NULL, other than to introduce obfuscation to an index when querying against it (i.e. ... WHERE NVL(FieldName,'Default Value') != 'test')???
-- Returns only 2 rows
-- Oracle doesn't differentiate from blank strings and NULL values
SELECT NULL AS Test_Nulls from dual
UNION
SELECT '' AS Test_Nulls from dual
UNION
SELECT 'Test' AS Test_Nulls from dual;
-- Returns "X"
-- Further proof Oracle doesn't differentiate from blank strings and NULL values
SELECT 'X' AS Test_Nulls
FROM dual
WHERE '' is NULL;
-- Returns "X"
-- Even further proof Oracle doesn't differentiate from blank strings and NULL values
SELECT NVL('','X') AS Test_Nulls
FROM dual;
-- Returns 3 rows
-- Oracle recognizes <Carriage Return> + <Line Feed>
SELECT '' AS Returned from dual
UNION
SELECT '
' AS Returned FROM dual
UNION
SELECT 'Test' AS Returned FROM dual;
Simple Answer: For any new database design work I do in Oracle, I will set all my varchar2 and nvarchar2 and char2 and nchar2 fields to not allow NULL and to have a default value of an empty string. I have always been suspicious of the naming convention of adding a "2" suffix, and the fact that it is not a common field type to other database platforms. To me, it seems like a last-minute desperate means that Oracle handled a situation in their early days like, "Oh well, ... we can't make it right, so we'll just give it a new name!" That's my 2-cents on this matter.
Oracle 12 introduced nice feature (which should have been there long ago btw!) - identity columns. So here's a script:
CREATE TABLE test (
a INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
b VARCHAR2(10)
);
-- Ok
INSERT INTO test (b) VALUES ('x');
-- Ok
INSERT INTO test (b)
SELECT 'y' FROM dual;
-- Fails
INSERT INTO test (b)
SELECT 'z' FROM dual UNION ALL SELECT 'zz' FROM DUAL;
First two inserts run without issues providing values for 'a' of 1 and 2. But the third one fails with ORA-01400: cannot insert NULL into ("DEV"."TEST"."A"). Why did this happen? A bug? Nothing like this is mentioned in the documentation part about identity column restrictions. Or am I just doing something wrong?
I believe the below query works, i havent tested!
INSERT INTO Test (b)
SELECT * FROM
(
SELECT 'z' FROM dual
UNION ALL
SELECT 'zz' FROM dual
);
Not sure, if it helps you any way.
For, GENERATED ALWAYS AS IDENTITY Oracle internally uses a Sequence only. And the options on general Sequence applies on this as well.
NEXTVAL is used to fetch the next available sequence, and obviously it is a pseudocolumn.
The below is from Oracle
You cannot use CURRVAL and NEXTVAL in the following constructs:
A subquery in a DELETE, SELECT, or UPDATE statement
A query of a view or of a materialized view
A SELECT statement with the DISTINCT operator
A SELECT statement with a GROUP BY clause or ORDER BY clause
A SELECT statement that is combined with another SELECT statement with the UNION, INTERSECT, or MINUS set operator
The WHERE clause of a SELECT statement
DEFAULT value of a column in a CREATE TABLE or ALTER TABLE statement
The condition of a CHECK constraint
The subquery and SET operations rule above should answer your Question.
And for the reason for NULL, when pseudocolumn(eg. NEXTVAL) is used with a SET operation or any other rules mentioned above, the output is NULL, as Oracle couldnt extract them in effect with combining multiple selects.
Let us see the below query,
select rownum from dual
union all
select rownum from dual
the result is
ROWNUM
1
1