Thread: How to implent the CONVERT ( data_type [ ( length ) ] , expression ) function in PostgreSQL

Dear

I'm doing a job about converting an expression of one data type to another.
In SQLServer, there'are two functions to do this job.

1. CAST ( expression AS data_type [ ( length ) ] )
2. CONVERT ( data_type [ ( length ) ] , expression )

However, In PostgreSQL, there's only the CAST ( expression AS data_type [ ( length ) ] ) function. I have tried the following two ways to implenting the CONVERT ( data_type [ ( length ) ] , expression ) function, but both are failed.

1. CREATE FUNCTION ..... 
The function's arguments can only be expressions but not data_type . 
2. Modifying the gram.y .....
The CONVERT ( data_type [ ( length ) ] , expression ) is in grammer conflict with the PostgreSQL self's convert(data,src_encoding_name,dest_encoding_name) function. And the PostgreSQL self's convert(data,src_encoding_name,dest_encoding_name) function cannot be used.

I wonder whether there's a better way to solve this problem. 
Any help will be appreciated.

Best Regards
Rohtodeveloper
El 02/11/14 a las 07:15, rohtodeveloper escibió:
> Dear
>
> I'm doing a job about converting an expression of one data type to
> another.
> In SQLServer, there'are two functions to do this job.
>

From which data_type to which one?  In psql, the option \dC, will give
you the list of supported casts.

Did you try the cast operand?

postgres=# select '1235'::int , 823::text, (now()::date)::text;
 int4 | text |    now
------+------+------------
 1235 | 823  | 2014-11-02
(1 row)



--
--
Emanuel Calvo                 http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services



On 11/02/2014 06:13 AM, Emanuel Calvo wrote:
>
> El 02/11/14 a las 07:15, rohtodeveloper escibió:
>> Dear
>>
>> I'm doing a job about converting an expression of one data type to
>> another.
>> In SQLServer, there'are two functions to do this job.
>>
>
>From which data_type to which one?  In psql, the option \dC, will give
> you the list of supported casts.
>
> Did you try the cast operand?
>
> postgres=# select '1235'::int , 823::text, (now()::date)::text;
>   int4 | text |    now
> ------+------+------------
>   1235 | 823  | 2014-11-02
> (1 row)

In addition if you are trying to do this in/to a table column there is:

http://www.postgresql.org/docs/9.3/interactive/sql-altertable.html

ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE
collation ] [ USING expression ]


>
>
>


--
Adrian Klaver
adrian.klaver@aklaver.com