开发者

How to change the date type in a Oracle Select?

I'm usualy use 开发者_开发百科PostgreSQL, but I'm currently doing it in Oracle.

I need to chage the data type of a column in a query(select), in PostgreSQL I usualy do it in this way:

select 1::varchar from table

Hoe can I do this in Oracle?

Best Regards,


convert to varchar

 select to_char(Field) from table

truncate varchar

 select substr(field, 1, 1) from table


As @Michael Pakhantsov points out, to_char works for converting to string. Likewise, to_date and to_timestamp are the standard when converting strings to dates and timestamps respectively. However, if you find that you need to perform a more exotic conversion (varchar2 to raw, for instance), then cast is your friend:

Number to string:

select cast(field as varchar2(30)) from table;

String to Raw:

select cast(field as raw(16)) from table;


Like @Allan I like to use cast(), as it can go from and to many types.
However, as the official documentation says,

CAST does not support LONG, LONG RAW, any of the LOB datatypes, or the Oracle-supplied types

So it cannot be used to convert to CLOB, for example, Oracle will throw

ORA-00932: inconsistent datatypes: expected - got CLOB

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜