HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Does NULL have a type?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
typenullhavedoes

Problem

Various sources (eg Wikipedia, PSOUG) state that Oracle's null does not have a type. Is this true?

What about other RDBMSs?

Solution

Oracle:

The null literal does not have a type, but

-
null can be cast to any type, and this may be necessary when

  • calling overloaded procedures or functions



-
controlling the return type of the decode function, eg:

select decode('A','B',to_char(null),'A','1') from dual;
DECODE('A','B',TO_CHAR(NULL),'A','1')
-------------------------------------
1

select decode('A','B',to_number(null),'A','1') from dual;
DECODE('A','B',TO_NUMBER(NULL),'A','1')
--------------------------------------- 
                                      1


  • controlling column types of set operators like union when the first query block includes a null



-
null values stored in the database always have a type:

create table t(n integer, s varchar(10));
insert into t values(null, null);

select decode('A','B',n,'A','1') from t; 
DECODE('A','B',N,'A','1')
-------------------------
                        1

select decode('A','B',s,'A','1') from t;
DECODE('A','B',S,'A','1')
-------------------------
1

Code Snippets

select decode('A','B',to_char(null),'A','1') from dual;
DECODE('A','B',TO_CHAR(NULL),'A','1')
-------------------------------------
1

select decode('A','B',to_number(null),'A','1') from dual;
DECODE('A','B',TO_NUMBER(NULL),'A','1')
--------------------------------------- 
                                      1
create table t(n integer, s varchar(10));
insert into t values(null, null);

select decode('A','B',n,'A','1') from t; 
DECODE('A','B',N,'A','1')
-------------------------
                        1

select decode('A','B',s,'A','1') from t;
DECODE('A','B',S,'A','1')
-------------------------
1

Context

StackExchange Database Administrators Q#4412, answer score: 8

Revisions (0)

No revisions yet.