Typecast integer or numeric to character in Postgresql

In order to typecast integer or numeric to character in postgresql we will be using cast() function. With the help of cast() function we will be able to typecast numeric or integer to character in postgresql. Let’s see how to

  • Typecast integer or numeric to character in Postgresql

We will be using books table.

Typecast integer or numeric to character in Postgresql 1

 

Typecast integer or numeric to character in Postgresql:

Method 1: Using :: to typecast

Column name followed by :: and followed by text is used to typecast total_pages column.

select *,total_pages::text as tot_page_char from books

So the resultant dataframe with total_pages typecasted will be

Typecast integer or numeric to character in Postgresql 2

 

Method 2: Using CAST()  function to typecast

CAST() function with Argument column_name as  text is used to typecast total_pages column.

select *,cast ( total_pages as Text) as tot_page_char from books

So the resultant dataframe with total_pages typecasted will be

Typecast integer or numeric to character in Postgresql 3