我想要一个postgresql函数来获取带有fils的单词数量

时间:2015-08-19 07:45:53

标签: sql postgresql plpgsql postgresql-9.2

我的金额为255.25。 然后该功能可以返回: - 两个hunder五十五25/100

3 个答案:

答案 0 :(得分:2)

您可以使用货币类型功能:

SELECT replace(cash_words('123'),'dollars and zero cents','');

返回

  

“一百二十三”

其他替代方案(未选中):

https://wiki.postgresql.org/wiki/Integer_to_Text https://wiki.postgresql.org/wiki/Numeric_to_English

答案 1 :(得分:0)

印度编号系统的功能...

CREATE OR REPLACE FUNCTION fx_numtowords(
    n bigint)
    RETURNS text
    LANGUAGE 'plpgsql'

AS $BODY$

DECLARE
  e TEXT;
BEGIN

  WITH Below20(Word, Id) AS
  (
    VALUES
      ('Zero', 0), ('One', 1),( 'Two', 2 ), ( 'Three', 3),
      ( 'Four', 4 ), ( 'Five', 5 ), ( 'Six', 6 ), ( 'Seven', 7 ),
      ( 'Eight', 8), ( 'Nine', 9), ( 'Ten', 10), ( 'Eleven', 11 ),
      ( 'Twelve', 12 ), ( 'Thirteen', 13 ), ( 'Fourteen', 14),
      ( 'Fifteen', 15 ), ('Sixteen', 16 ), ( 'Seventeen', 17),
      ('Eighteen', 18 ), ( 'Nineteen', 19 )
   ),
   Below100(Word, Id) AS
   (
      VALUES
       ('Twenty', 2), ('Thirty', 3),('Forty', 4), ('Fifty', 5),
       ('Sixty', 6), ('Seventy', 7), ('Eighty', 8), ('Ninety', 9)
   )
   SELECT
     CASE
      WHEN n = 0 THEN  ''
      WHEN n BETWEEN 1 AND 19
        THEN (SELECT Word FROM Below20 WHERE ID=n)
     WHEN n BETWEEN 20 AND 99
       THEN  (SELECT Word FROM Below100 WHERE ID=n/10) ||  ' '  ||
             fx_numtowords( n % 10)
     WHEN n BETWEEN 100 AND 999
       THEN  (fx_numtowords( n / 100)) ||  ' Hundred and ' ||--case when n>199 then ' Hundred and '  else ' Hundred ' end ||
           fx_numtowords( n % 100)
     WHEN n BETWEEN 1000 AND 99999
       THEN  (fx_numtowords( n / 1000)) || ' Thousand ' ||-- case when n> 1999  then ' Thousands ' else ' Thousand ' end ||
           fx_numtowords( n % 1000)
     WHEN n BETWEEN 100000 AND 9999999
       THEN  (fx_numtowords( n / 100000)) || ' Lakh ' ||--case when n> 1999999 then ' Millions ' else ' Million ' end ||
           fx_numtowords( n % 100000)
     WHEN n BETWEEN 10000000 AND 99999999999999
       THEN  (fx_numtowords( n / 10000000)) || ' Crore ' ||--case when n> 1999999999 then ' Billions ' else ' Billion ' end||
           fx_numtowords( n % 10000000)     
          ELSE ' INVALID INPUT' END INTO e;

  e := RTRIM(e);

  IF RIGHT(e,1)='-' THEN
    e := RTRIM(LEFT(e,length(e)-1));
  END IF;

  RETURN e;
END;

$BODY$;

答案 2 :(得分:-1)

CREATE OR REPLACE FUNCTION fx_letras(n BIGINT) RETURNS TEXT AS
$$
DECLARE
  e TEXT;
BEGIN

  WITH Below20(Word, Id) AS
  (
    VALUES
      ('Cero', 0), ('Uno', 1),( 'Dos', 2 ), ( 'Tres', 3),
      ( 'Cuatro', 4 ), ( 'Cinco', 5 ), ( 'Seis', 6 ), ( 'Siete', 7 ),
      ( 'Ocho', 8), ( 'Nueve', 9), ( 'Diez', 10), ( 'Once', 11 ),
      ( 'Doce', 12 ), ( 'Trece', 13 ), ( 'Catorce', 14),
      ( 'Quince', 15 ), ('Dieciséis', 16 ), ( 'Diecisiete', 17),
      ('Dieciocho', 18 ), ( 'Diecinueve', 19 )
   ),
   Below100(Word, Id) AS
   (
      VALUES
       ('Veinte', 2), ('Treinta', 3),('Cuarenta', 4), ('Cincuenta', 5),
       ('Sesenta', 6), ('Setenta', 7), ('Ochenta', 8), ('Noventa', 9)
   )
   SELECT
     CASE
      WHEN n = 0 THEN  ''
      WHEN n BETWEEN 1 AND 19
        THEN (SELECT Word FROM Below20 WHERE ID=n)
     WHEN n BETWEEN 20 AND 99
       THEN  (SELECT Word FROM Below100 WHERE ID=n/10) ||  ' y '  ||
             fx_letras( n % 10)
     WHEN n BETWEEN 100 AND 999
       THEN  (fx_letras( n / 100)) ||  case when n>199 then ' Cientos '  else ' Ciento ' end ||
           fx_letras( n % 100)
     WHEN n BETWEEN 1000 AND 999999
       THEN  (fx_letras( n / 1000)) || case when n> 1999  then ' Miles ' else ' Mil ' end ||
           fx_letras( n % 1000)
     WHEN n BETWEEN 1000000 AND 999999999
       THEN  (fx_letras( n / 1000000)) || case when n> 1999999 then ' Millones ' else ' Millón ' end ||
           fx_letras( n % 1000000)
     WHEN n BETWEEN 1000000000 AND 999999999999
       THEN  (fx_letras( n / 1000000000)) || case when n> 1999999999 then ' Billones ' else ' Billón ' end||
           fx_letras( n % 1000000000)
     WHEN n BETWEEN 1000000000000 AND 999999999999999
       THEN  (fx_letras( n / 1000000000000)) || case when n> 1999999999999 then  ' Trillones ' else ' Trillón ' end  ||
           fx_letras( n % 1000000000000)
    WHEN n BETWEEN 1000000000000000 AND 999999999999999999
       THEN  (fx_letras( n / 1000000000000000)) || case when n> 1999999999999999 then ' Quadrilliones ' else ' Quadrillion ' end ||
           fx_letras( n % 1000000000000000)
    WHEN n BETWEEN 1000000000000000000 AND 999999999999999999999
       THEN  (fx_letras( n / 1000000000000000000)) || case when n>1999999999999999999 then ' Quintilliones ' else ' Quintillion ' end ||
           fx_letras( n % 1000000000000000000)
          ELSE ' INVALID INPUT' END INTO e;

  e := RTRIM(e);

  IF RIGHT(e,1)='-' THEN
    e := RTRIM(LEFT(e,length(e)-1));
  END IF;

  RETURN e;
END;
$$ LANGUAGE PLPGSQL;