From: | Aleksander Kmetec <aleksander(dot)kmetec(at)intera(dot)si> |
---|---|
To: | Peter Hunsberger <peter(dot)hunsberger(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Find difference between two Text fields |
Date: | 2009-07-25 03:01:57 |
Message-ID: | 4A6A75A5.4070203@intera.si |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Hi,
there might be a better solution out there, but it seemed like an interesting problem so I wrote this function:
CREATE OR REPLACE FUNCTION stringdiff(text, text)
RETURNS TEXT
AS $$
SELECT array_to_string(ARRAY(
SELECT
CASE WHEN substring($1 FROM n FOR 1) = substring($2 FROM n FOR 1)
THEN ' '
ELSE substring($2 FROM n FOR 1)
END
FROM generate_series(1, character_length($1)) as n), '');
$$ language sql;
Use it like this:
SELECT stringdiff('aaaaaaaaaa', 'axaaacaaza');
stringdiff
------------
x c z
Regards,
Aleksander
Peter Hunsberger wrote:
> Can anyone give me a way to find the difference between two text
> fields on a character by character basis. Essentially, I'd like to
> logically AND the two together and for any position that has a
> non-zero result show whatever character is in that position for the
> second string. The solution can be postgres specific but something
> approaching ANSI SQL would also be helpful (if possible).
>
From | Date | Subject | |
---|---|---|---|
Next Message | Jan-Erik | 2009-07-25 05:50:02 | Re: split string by special characters |
Previous Message | Brian A. Seklecki | 2009-07-25 02:41:23 | Re: Disable databse listing for non-superuser (\l) ? |