cancel
Showing results for 
Search instead for 
Did you mean: 

How to return a reversed integer with pl/pgsql function

Adventurer

How to return a reversed integer with pl/pgsql function

I am new to postgresql. I have  a question: how to return a reversed integer by function. I  code like this:

CREATE FUNCTION REV_NO () RETURNS int AS $$
SELECT 1234 AS val, REVERSED(CAST(1234 AS INT)) AS reveresed
$$ LANGUAGE SQL;

 

select * from rev_no;

Is this correct function. Please point where is the problem? Thanks

 

3 REPLIES 3
EDB Team Member

Re: How to return a reversed integer with pl/pgsql function

Hi,

 

Please try to create the function using the below syntax and let us know if this helps:

 

postgres=# CREATE FUNCTION REV_NO () RETURNS int AS $$
select cast(reverse('1234') as int) as reversed
$$ LANGUAGE SQL;
CREATE FUNCTION
postgres=# select REV_NO();
 rev_no 
--------
   4321
(1 row)

--Ankit Shukla

Adventurer

Re: How to return a reversed integer with pl/pgsql function

Ankit:

 

Thanks a lot for your advice. I have done it with this code and it almost like your code. But your code is more well-knitted than mine.

 

CREATE OR REPLACE FUNCTION REV_NO () RETURNS int AS $$

SELECT CAST(REVERSE(CAST(0358 AS VARCHAR)) AS INTEGER) AS reveresed

$$ LANGUAGE SQL;

 

SELECT REV_NO();

        REV_NO

----------------

1         853

 

Highlighted
Adventurer

Re: How to return a reversed integer with pl/pgsql function

Ankit:

 

Thanks a lot for your advice. I have done it with this code and it almost like your code. But your code is more well-knitted than mine.

 

CREATE OR REPLACE FUNCTION REV_NO () RETURNS int AS $$

SELECT CAST(REVERSE(CAST(0358 AS VARCHAR)) AS INTEGER) AS reveresed

$$ LANGUAGE SQL;



SELECT REV_NO();

        REV_NO

----------------

1         853