Thread: String reverse funtion?

String reverse funtion?

From
ginkgo36
Date:
Hello everyone

I have to reverse a string like EA;BX;CA to CA;BX;EA. or EA,BX,CA to
CA,BX,EA

Is there any function to do this?

Thanks all!



--
View this message in context: http://postgresql.1045698.n5.nabble.com/String-reverse-funtion-tp5773871.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Fwd: String reverse funtion?

From
John McKown
Date:
Forgot to reply to list. Sorry. Perhaps somebody here knows of an "array_reverse" type function.

---------- Forwarded message ----------
From: John McKown <john.archie.mckown@gmail.com>
Date: Fri, Oct 11, 2013 at 7:53 AM
Subject: Re: [GENERAL] String reverse funtion?
To: ginkgo36 <ginkgo56@gmail.com>


I don't see one. Looks like you will need to write your own function. I got close with a kludge. But I cannot find a base function which will "reverse" the order of elements in an array. If there were such a one, which I will call "array_reverse" in the example, then you could do:

select array_to_string( array_reverse( regexp_split_to_array(string_delimited_with_semicolons,';') ),';');

This would split the string into an array, where each element is delimited with a semi-colon - regexp_split_to_array
Reverse the order of the array with the __missing__ array_reverse function.
Combine back into a string with array_to_string.

But, given the lack of "array_reverse" or something equivalent, I guess you need to "roll your own" function.



On Wed, Oct 9, 2013 at 10:59 AM, ginkgo36 <ginkgo56@gmail.com> wrote:
Hello everyone

I have to reverse a string like EA;BX;CA to CA;BX;EA. or EA,BX,CA to
CA,BX,EA

Is there any function to do this?

Thanks all!



--
View this message in context: http://postgresql.1045698.n5.nabble.com/String-reverse-funtion-tp5773871.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



--
This is clearly another case of too many mad scientists, and not enough hunchbacks.

Maranatha! <><
John McKown



--
This is clearly another case of too many mad scientists, and not enough hunchbacks.

Maranatha! <><
John McKown

Re: Fwd: String reverse funtion?

From
ginkgo36
Date:
Hi everyone,
Please hepl me this function:

1. I want to sort string follow anphabet and I used this query:
select string_agg(x, ';') from (select
trim(unnest(regexp_split_to_array('ECD FORM; BODY; PREDILUTED; CHROMO-GENIC;
AUTO;RABBIT; FORMAT',';'))) x order by x) a;

-- result: AUTO; BODY; CHROMOGENIC; ECD FORM;  FORMAT; PREDILUTED;  RABBIT
-->I expected this rusult

In my database I have a column with alot of rows data. I want that query
become a function to more easy to using. But I can not write a function :(.
please hepl me.
For example, I have column "data_text" with data like this:
Row 1: AUTO; BODY; PREDILUTED; ECD FORM; RABBIT; FORMAT; CHROMOGENIC
Row 2: ECD FORM; BODY; PREDILUTED; CHROMO-GENIC; AUTO; RABBIT; FORMAT
Row 3: FORMAT; ECD FORM; AUTO
Row 3: ANHYDROUS; DENATURED; PREDILUTED; CHROMOGENIC

When I run funtion, the result:
Row 1: AUTO; BODY; CHROMOGENIC; ECD.FORM; FORMAT; PREDILUTED; RABBIT
Row 2: AUTO; BODY; CHROMO-GENIC; ECD FORM; FORMAT; PREDILUTED; RABBIT
Row 3: AUTO; ECD FORM; FORMAT
Row 4: ANHYDROUS; CHROMOGENIC; DENATURED; PREDILUTED

Thank you and best regards,



--
View this message in context: http://postgresql.1045698.n5.nabble.com/String-reverse-funtion-tp5773871p5774642.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: Fwd: String reverse funtion?

From
David Johnston
Date:
ginkgo36 wrote
> But I can not write a function :(. please hepl me.
> For example, I have column "data_text" with data like this:
> Row 1: AUTO; BODY; PREDILUTED; ECD FORM; RABBIT; FORMAT; CHROMOGENIC
> Row 2: ECD FORM; BODY; PREDILUTED; CHROMO-GENIC; AUTO; RABBIT; FORMAT
> Row 3: FORMAT; ECD FORM; AUTO
> Row 3: ANHYDROUS; DENATURED; PREDILUTED; CHROMOGENIC
>
> When I run funtion, the result:
> Row 1: AUTO; BODY; CHROMOGENIC; ECD.FORM; FORMAT; PREDILUTED; RABBIT
> Row 2: AUTO; BODY; CHROMO-GENIC; ECD FORM; FORMAT; PREDILUTED; RABBIT
> Row 3: AUTO; ECD FORM; FORMAT
> Row 4: ANHYDROUS; CHROMOGENIC; DENATURED; PREDILUTED

Assuming what you show above is your goal writing such a function is
straight-forward.  Basically you take your existing SQL and wrap it in a
CREATE FUNCTION.  The input would be of type "text" as would the output.
Inside the function you do all the same split/unnest/trim/string_agg actions
as you show above.

Start you exploration on the syntax and mechanics here:

http://www.postgresql.org/docs/9.3/interactive/xfunc.html

While you could use pl/pgsql a simple SQL function is sufficient for your
needs.

Post a specific question with your attempted "CREATE FUNCTION" if you get
stuck.

David J.




--
View this message in context: http://postgresql.1045698.n5.nabble.com/String-reverse-funtion-tp5773871p5774647.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.