Snippets / Postgres / cast

cast

By Marcelo Fernandes Aug 10, 2017

We use the cast function to change the type of the column.

SELECT description, cast((cost_price * 100) AS int) AS "Cost Price"
 FROM item;


Before / After

SELECT description, cost_price * 100 FROM item;

 description | ?column?
---------------+--------------
 Wood Puzzle | 1523.00
 Rubic Cube  | 745.00
 Linux CD    | 199.00
 Tissues     | 211.00
 Picture Fr  | 754.00
 Fan Small   | 923.00
 Fan Large   | 1336.00
 Toothbrush  | 75.00
 Roman Coin  | 234.00
 Carrier Bag | 1.00
 Speakers    | 1973.00


SELECT description, cast((cost_price * 100) AS int) AS "Cost Price"
 FROM item;

description | Cost Price
--------------+--------------
Wood Puzzle   | 1523
Rubic Cube    | 745
Linux CD      | 199
Tissues       | 211
Picture Frame | 754
Fan Small     | 923
Fan Large     | 1336
Toothbrush    | 75
Roman Coin    | 234
Carrier Bag   | 1
Speakers      | 1973


Notes