PostgreSQL การเขียนฟังก์ชั่น
เป็นตัวอย่างการเขียนฟังก์ชั่นในการตัดตัวอักษรที่ไม่ใช่ตัวเลขออกจากคำที่ต้องการ
create or replace function test_valid_number(arg1 varchar)
returns float AS $$
DECLARE len_char int;
DECLARE loop_char int;
DECLARE temp_char1 varchar;
DECLARE temp_char2 varchar;
DECLARE temp_i int;
DECLARE temp_ret float;
BEGIN
/*
ASCII
46 .
48-57 0-9
*/
if (upper(arg1)='NIL' or upper(arg1)='ND') then
RETURN 0.01;
else
len_char=char_length(arg1);
loop_char=0;
temp_ret=0;
temp_char2='';
FOR loop_char IN 1..len_char LOOP
temp_char1=SUBSTRING(arg1 from loop_char for 1);
temp_i=ASCII(temp_char1);
if (temp_i >= 48 and temp_i <=57) or (temp_i=46) then temp_char2=temp_char2 || temp_char1; --else --RAISE NOTICE 'No Number'; end if; END LOOP; if (char_length(temp_char2) > 0 ) then
temp_ret=cast(temp_char2 as float);
else
temp_ret=0;
end if;
RETURN temp_ret;
end if;
END;
$$ LANGUAGE plpgsql;
returns float AS $$
DECLARE len_char int;
DECLARE loop_char int;
DECLARE temp_char1 varchar;
DECLARE temp_char2 varchar;
DECLARE temp_i int;
DECLARE temp_ret float;
BEGIN
/*
ASCII
46 .
48-57 0-9
*/
if (upper(arg1)='NIL' or upper(arg1)='ND') then
RETURN 0.01;
else
len_char=char_length(arg1);
loop_char=0;
temp_ret=0;
temp_char2='';
FOR loop_char IN 1..len_char LOOP
temp_char1=SUBSTRING(arg1 from loop_char for 1);
temp_i=ASCII(temp_char1);
if (temp_i >= 48 and temp_i <=57) or (temp_i=46) then temp_char2=temp_char2 || temp_char1; --else --RAISE NOTICE 'No Number'; end if; END LOOP; if (char_length(temp_char2) > 0 ) then
temp_ret=cast(temp_char2 as float);
else
temp_ret=0;
end if;
RETURN temp_ret;
end if;
END;
$$ LANGUAGE plpgsql;
ทุกท่านอาจจะนำไปประยุกต์กับการเขียนฟังก์ชั่นในการใช้งานสำหรับงานที่ท่านต้องการได้
ความคิดเห็น
แสดงความคิดเห็น