On May 3, 2009, at 9:00 AM, Craig Ringer wrote:
CREATE OR REPLACE FUNCTION luhn_verify(int8) RETURNS boolean AS $$
SELECT
-- Add the digits, doubling odd-numbered digits (counting left with
-- least significant as zero), and see if the sum is evenly
-- divisible by zero.
I think you mean divisible by 10 here, numbers are generally not
divisible by zero ;)
Regardless, thanks for posting these functions, I'm sure they'll come
in handy some time.
MOD(SUM(
-- Extract digit `n' counting left from least significant as
zero
MOD( ( $1::int8 / (10^n)::int8 ), 10::int8)
-- Double odd-numbered digits
* (MOD(n,2) + 1)
), 10) = 0
FROM generate_series(0, ceil(log( $1 ))::integer - 1) AS n;
$$ LANGUAGE 'SQL'
IMMUTABLE
STRICT;
COMMENT ON FUNCTION luhn_verify(int8) IS 'Return true iff the last
digit
of the input is a correct check digit for the rest of the input
according to Luhn''s algorithm.'
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.
!DSPAM:737,49fd82b6129742129210600!
--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general