AbIr Chanda
AbIr Chanda

Reputation: 739

error while executing

What is the difference between 2 and '2' in Oracle?

Different datatype ?

select 1 from tab union select '2' from tab;

error occurred

Upvotes: 1

Views: 43

Answers (2)

are
are

Reputation: 2615

bit more info to @Mahmoud Gamal answer see the simple test

create table t1 as select 1 as col1 from dual;
create table t2 as select '1' as col1 from dual;

and then in SQL Plus we can see that t1.col1 is numeric, but t2.col1 is CHAR(1)

Connected to Oracle Database 11g Express Edition Release 11.2.0.2.0 
Connected as ***

SQL> desc t1;
Name Type   Nullable Default Comments 
---- ------ -------- ------- -------- 
COL1 NUMBER Y                         

SQL> desc t2;
Name Type    Nullable Default Comments 
---- ------- -------- ------- -------- 
COL1 CHAR(1) Y                         

SQL> 

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

Yes, different data types; 2 is an integer, where '2' is a string literal, so they are in different data types, thats why you are getting an error.

When you use UNION the data types should be matching; they should be either integer or string literals like this:

select 1 from tab union select 2 from tab;

Upvotes: 3

Related Questions