Various sources (eg Wikipedia, PSOUG) state that Oracle's null
does not have a type. Is this true?
What about other RDBMSs?
Oracle:
The null
literal does not have a type, but
null
can be cast to any type, and this may be necessary when
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
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
SQL Server, int
SELECT NULL AS foo INTO dbo.bar
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'bar'
DROP TABLE dbo.bar
MySQL, binary(0)
CREATE TABLE mydb.foo (select NULL AS bar);
EXPLAIN mydb.foo;
DROP TABLE mydb.foo;
Oracle it is in some sense some string type.
Thats what ADO Reader tells me. here is a Powershell Script:
[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient")
$ConnectionString = "Data Source=myTNS;User ID=myUSER;Password=myPassword"
$conn=new-object System.Data.OracleClient.OracleConnection
$conn.ConnectionString=$ConnectionString
$conn.Open()
$sql = "Select NULL xx from DUAL"
$cmd=new-object System.Data.OracleClient.OracleCommand($sql,$conn)
$r = $cmd.ExecuteReader()
$r.GetSchemaTable() | % { $_
}
That gives
ColumnName : XX
ColumnOrdinal : 0
ColumnSize : 0
NumericPrecision : 0
NumericScale : 0
DataType : System.String
ProviderType : 22
IsLong : False
AllowDBNull : True
IsAliased :
IsExpression :
IsKey :
IsUnique :
BaseSchemaName :
BaseTableName :
BaseColumnName :
ProviderSpecificDataType : System.Data.OracleClient.OracleString
Note the line
ProviderSpecificDataType : System.Data.OracleClient.OracleString
postgres:
create table foo as select null as bar;
WARNING: column "bar" has type "unknown"
DETAIL: Proceeding with relation creation anyway.
postgres=> \d foo
Column | Type | Modifiers
--------+---------+-----------
bar | unknown |