4
  • Oracle version : 12.1.0.2.0
  • SQL Server version : 2012

My code:

select COALESCE (null, '') 
from dual

in Oracle returns null result, but

select COALESCE (null, '')

in SQL Server returns a '' result.

Why did this result differ?

They are ANSI standards, shouldn't it be the same?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Wei Lin
  • 3,591
  • 2
  • 20
  • 52

4 Answers4

7

In Oracle document about NULL

Oracle Database treats a character value with a length of zero as null.

Oracle internally changes the empty string to NULL values. Oracle simply won't let insert an empty string.

select null from dual

the same as

select '' from dual

They are all return NULL.

So when you use select COALESCE (null,'') from dual it will translate to select COALESCE (null,null) from dual in Oracle.

sqlfiddle

Here is a link talk about this.

Why does Oracle 9i treat an empty string as NULL?

D-Shih
  • 44,943
  • 6
  • 31
  • 51
3

As per the Oracle docs:

Oracle Database treats a character value with a length of zero as null.

Which is not ANSI standard.

Dale K
  • 25,246
  • 15
  • 42
  • 71
3

Assigning a zero-length string to a field results in NULL when dealing with a variable-length character type (e.g. VARCHAR or VARCHAR2). However, when a zero-length string is assigned to a fixed-length character type (e.g. CHAR) the result is that the field is blank-padded to full defined length, as required for the CHAR data type. See this AskTom question for further info.

Dale K
  • 25,246
  • 15
  • 42
  • 71
2

Have you tried the following code in Oracle and SQL Server:

select COALESCE (null,' ') from dual
select COALESCE (null,' ')

it returns not null result in both Oracle and SQL Server

The main issue is how Oracle and SQL Server treat the 2nd expression ''. In Oracle, it is treated as NULL but SQL Server treats it as an empty space.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63