String reverse funtion?

Started by ginkgo36over 12 years ago9 messagesgeneral
Jump to latest
#1ginkgo36
ginkgo56@gmail.com

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

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: ginkgo36 (#1)
Re: String reverse funtion?

ginkgo36 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!

No. You will have to write your own.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/String-reverse-funtion-tp5773871p5773887.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

#3John Meyer
johnmeyer@pueblocomputing.com
In reply to: David G. Johnston (#2)
Re: String reverse funtion?

On 10/9/2013 11:52 AM, David Johnston wrote:

ginkgo36 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!

No. You will have to write your own.

David J.

Based upon the example, it's probably very easy to use a split/explode
in your language of choice (VB.NET, perl, python, etc).

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

#4Pavel Stehule
pavel.stehule@gmail.com
In reply to: John Meyer (#3)
Re: String reverse funtion?

2013/10/9 John Meyer <johnmeyer@pueblocomputing.com>

On 10/9/2013 11:52 AM, David Johnston wrote:

ginkgo36 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!

No. You will have to write your own.

David J.

Based upon the example, it's probably very easy to use a split/explode in
your language of choice (VB.NET, perl, python, etc).

or SQL

select string_agg(u, ';' order by r desc)
from (select row_number() over () r, u
from unnest(string_to_array('EA;BX;CA',';')) u) x;
string_agg
────────────
CA;BX;EA
(1 row)

Regards

Pavel

Show quoted text

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

#5John Meyer
johnmeyer@pueblocomputing.com
In reply to: Pavel Stehule (#4)
Re: String reverse funtion?

That works too. Point being ginkgo36 has his solution.

Show quoted text

On 10/9/2013 12:07 PM, Pavel Stehule wrote:

2013/10/9 John Meyer <johnmeyer@pueblocomputing.com
<mailto:johnmeyer@pueblocomputing.com>>

On 10/9/2013 11:52 AM, David Johnston wrote:

ginkgo36 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!

No. You will have to write your own.

David J.

Based upon the example, it's probably very easy to use a
split/explode in your language of choice (VB.NET <http://VB.NET&gt;,
perl, python, etc).

or SQL

select string_agg(u, ';' order by r desc)
from (select row_number() over () r, u
from unnest(string_to_array('EA;BX;CA',';')) u) x;
string_agg
────────────
CA;BX;EA
(1 row)

Regards

Pavel

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

#6ginkgo36
ginkgo56@gmail.com
In reply to: Pavel Stehule (#4)
Re: String reverse funtion?

Thanks alot everyone,
Actually, I just start learning about Programming language. So, each your
solution is a good suggestion for me to learning. Thanks so much.
Please help me one User-defined Functions to solves this when my data like
this:
EA;BX;CA
CA;EA
BX;EA
And when run UDFs, output is:
CA;BX;EA
EA;CA
EA;BX

One again, thanks so much

Pavel Stehule wrote

Based upon the example, it's probably very easy to use a split/explode in
your language of choice (VB.NET, perl, python, etc).

or SQL

select string_agg(u, ';' order by r desc)
from (select row_number() over () r, u
from unnest(string_to_array('EA;BX;CA',';')) u) x;
string_agg
────────────
CA;BX;EA
(1 row)

Regards

Pavel

--
Sent via pgsql-general mailing list (

pgsql-general@

)

To make changes to your subscription:
http://www.postgresql.org/**mailpref/pgsql-general&amp;lt;http://www.postgresql.org/mailpref/pgsql-general&amp;gt;

--
View this message in context: http://postgresql.1045698.n5.nabble.com/String-reverse-funtion-tp5773871p5773950.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

#7John McKown
john.archie.mckown@gmail.com
In reply to: ginkgo36 (#1)
Fwd: String reverse funtion?

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

#8ginkgo36
ginkgo56@gmail.com
In reply to: John McKown (#7)
Re: Fwd: String reverse funtion?

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.

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

#9David G. Johnston
david.g.johnston@gmail.com
In reply to: ginkgo36 (#8)
Re: Fwd: String reverse funtion?

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.

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