Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle "create table as" null value

Tags:

oracle

If you create an Oracle table using "create as" where one of your fields is null you will get the error:

ORA-01723: zero-length columns are not allowed

Example query:

create table mytable as 
select 
    field_a, 
    null brand_new_field
from anothertable;

How can you get around this?

like image 660
Marcus Leon Avatar asked May 24 '16 15:05

Marcus Leon


People also ask

How do you create a NULL table in SQL?

Syntax. The basic syntax of NULL while creating a table. SQL> CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); Here, NOT NULL signifies that column should always accept an explicit value of the given data type.

Can we insert null value in Oracle?

The simplest way to put NULL into any column, regardless of the datatype, is: INSERT INTO emp (hiredate) VALUES (NULL); Don't use single-quotes around NULL. Putting it in single-quotes makes it a 4-character string value.

How do you NULL in Oracle?

Here is an example of how to use the Oracle IS NULL condition in a SELECT statement: SELECT * FROM suppliers WHERE supplier_name IS NULL; This Oracle IS NULL example will return all records from the suppliers table where the supplier_name contains a null value.


1 Answers

Figured it out need to use cast(null as datatype)

create table mytable as 
select 
    field_a, 
    cast(null as varchar(1)) brand_new_field
from anothertable;

Some more info here.

like image 91
Marcus Leon Avatar answered Nov 05 '22 11:11

Marcus Leon