Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting to custom type in SELECT query

I am new to PostgreSQL and absolutely lost here. (Guess even the title is absolutely wrong...)

I have e.g. this:

postgres=# SELECT round(10000::decimal/100, 4);
  round   
----------
 100.0000
(1 row)

Is there an easy way to get the same result using a custom type:

postgres=# SELECT 10000::my_type;
----------
 100.0000
(1 row)

1 Answers

I think it depends on the type. For example, the following works for me on Postgres 9.3

# CREATE TYPE foo AS (num INT);
CREATE TYPE

# SELECT 300::foo;
ERROR:  cannot cast type integer to foo
LINE 1: SELECT 300::foo

# SELECT (ROW(300)::foo).num;
 num 
-----
 300
(1 row)
like image 129
Hitobat Avatar answered Feb 01 '26 11:02

Hitobat