Skip to Main Content
  • Questions
  • Why does data type change while using create as select

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Such.

Asked: July 04, 2019 - 2:50 am UTC

Last updated: July 04, 2019 - 3:05 pm UTC

Version: 11G

Viewed 1000+ times

You Asked

We are in the process of creating masked data using PL/SQL.We adapted Create as select as one of the approaches to masked the data in the target table.

Below is sample source table structure

COLUMN NAME DATATYPE
EMPID NUMBER(8)
NAME VARCHAR2(40)
DOB TIMESTAMP(6)

While creating the target table we are masking NAME and DOB columns and inputting the masked data in the target table.

Below is the target table structure after masking

COLUMN NAME DATATYPE
EMPID NUMBER(8)
NAME VARCHAR2(100)
DOB DATE


Please help us understand why there is a difference in the source and target table structure using masking for Create As select .

and Chris said...

CTAS infers data types based on the values your subquery returns.

To ensure they have a specific data type, cast them:

create table t_copy as 
  select cast ( c1 as ... ) c1 ,
         cast ( c2 as ... ) c2 ,
         ...
  from   t;


While creating the target table we are masking NAME and DOB columns

How exactly are you doing this masking? By any chance are the masked named up to 100 characters long, and the masked DOBs DATEs?

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.