前几天一直有人在问,如何在Greenplum里面实现身份证号15位到18位的转换函数,今天网上搜了一下,看了看大概的逻辑,做了个简单实现,转换逻辑不懂的,请自行百度吧,这里放出函数内容,供大家使用:
函数:
-- Function: idcard15to18(character)
-- DROP FUNCTION idcard15to18(character);
CREATE OR REPLACE FUNCTION idcard15to18(card character)
RETURNS character AS
$BODY$declare
W int[];
A char[];
S integer;
RESULTS char(18);
begin
if CARD is null then
return '';
end if;
if length(CARD) <> 15 then
return CARD;
end if;
W := array[7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
A := array['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
RESULTS := substr(CARD, 1, 6)||'19'||substr(CARD, 7, 9);
S := 0;
begin
for i in 1..17 loop
S := S+substr(RESULTS, i, 1)::int*W[i];
end loop;
exception
when Others then
return '';
end;
S := mod(S,11);
RESULTS := RESULTS||A[S+1];
return(RESULTS);
end;$BODY$
LANGUAGE plpgsql VOLATILE;
ALTER FUNCTION idcard15to18(character)
OWNER TO dgadmin;
COMMENT ON FUNCTION idcard15to18(character) IS '身份证15位转18位函数';
示例:
tpch1g=# select idcard15to18('123456789123456');
idcard15to18
--------------------
12345619789123456X
(1 row)
End~