1. SQL / Говнокод #118

    −370.9

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    CREATE OR REPLACE FUNCTION hex_inc(in_str character, n integer)
    RETURNS character AS
    $BODY$
    DECLARE x varchar;
    BEGIN
    x=in_str;
    if (substring(in_str from n for n+1)='0') or (substring(in_str from n for n+1)='1') or (substring(in_str from n for n+1)='2')
    or (substring(in_str from n for n+1)='3') or (substring(in_str from n for n+1)='4') or (substring(in_str from n for n+1)='5')
    or (substring(in_str from n for n+1)='6') or (substring(in_str from n for n+1)='7') or (substring(in_str from n for n+1)='8')
    or (substring(in_str from n for n+1)='A') or (substring(in_str from n for n+1)='B') or (substring(in_str from n for n+1)='C')
    or (substring(in_str from n for n+1)='D') or (substring(in_str from n for n+1)='E') THEN
    x=raplace_char(n,x,chr((ascii(substring(in_str from n for n+1))+1))); -- inc() только для string
    ELSIF (substring(in_str from n for n+1)='9') THEN
    x=raplace_char(n,x,'A'); -- ну т.к. это уже 10
    ELSIF (substring(in_str from n for n+1)='F') THEN
    x=raplace_char(n,x,'0'); -- ну т.к. это +1 разряд :)
    x=hex_inc(x,n-1);
    END IF;
    RETURN x;
    END;
    $BODY$
    LANGUAGE 'plpgsql' VOLATILE
    COST 100;
    
    CREATE OR REPLACE FUNCTION raplace_char(n integer, in_str character, in_char character)
    RETURNS character AS
    $BODY$
    Begin
    IF ( n = char_length(in_str) ) THEN
    RETURN substring(in_str from 1 for n-1) || in_char;
    ELSIF (n = 1) THEN
    RETURN in_char || substring(in_str from 2 for char_length(in_str)-1) ;
    ELSE
    RETURN substring(in_str from 1 for n-1) || in_char || substring(in_str from n+1 for char_length(in_str)-n);
    END IF;
    END;
    $BODY$
    LANGUAGE 'plpgsql' STABLE
    COST 100;

    Язык PL/PgSQL
    Функция должна делать inc числу, записанному в строке в шестнадцатеричном виде

    Запостил: guest, 12 Декабря 2008

    Комментарии (1) RSS

    Добавить комментарий