Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the date type in a Oracle Select?

Tags:

types

oracle

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,

like image 663
André Avatar asked Dec 13 '22 16:12

André


2 Answers

convert to varchar

 select to_char(Field) from table

truncate varchar

 select substr(field, 1, 1) from table
like image 189
Michael Pakhantsov Avatar answered Jan 08 '23 23:01

Michael Pakhantsov


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 image 38
Allan Avatar answered Jan 09 '23 01:01

Allan