代码之家  ›  专栏  ›  技术社区  ›  Michał Niklas

如何设置PostgreSQL的千位分隔符?

  •  11
  • Michał Niklas  · 技术社区  · 16 年前

    我想用千位分隔符格式化长数字。可以使用 to_char

    SELECT TO_CHAR(76543210.98, '999G999G990D00')
    

    但是,当我的PostgreSQL服务器使用UTF-8编码的波兰版本的Windows时,这样的SELECT结尾是:

    ERROR:  invalid byte sequence for encoding "UTF8": 0xa0
    HINT:  This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
    

    收件人字符 G 描述为: .

    作为一种解决方法,我使用空间而不是 G 在格式字符串中,但我认为应该有办法像Oracle一样设置千位分隔符:

    ALTER SESSION SET NLS_NUMERIC_CHARACTERS=', ';
    

    2 回复  |  直到 8 年前
        1
  •  17
  •   Ali Akbar    11 年前

    如果你使用 psql ,可以执行以下操作:

    \pset numericlocale
    

    例子:

    test=# create temporary table a (a numeric(20,10));
    CREATE TABLE
    
    test=# insert into a select random() * 1000000 from generate_series(1,3);
    INSERT 0 3
    
    test=# select * from a;
             a         
    -------------------
     287421.6944910590
     140297.9311533270
     887215.3805568810
    (3 rows)
    
    test=# \pset numericlocale
    Showing locale-adjusted numeric output.
    
    test=# select * from a;
             a          
    --------------------
     287.421,6944910590
     140.297,9311533270
     887.215,3805568810    
    (3 rows)
    
        2
  •  2
  •   Mike Sherrill 'Cat Recall'    15 年前

    我确信错误消息是真的:0xa0不是有效的UTF-8字符。

    sandbox=# show client_encoding;
     client_encoding
    -----------------
     UTF8
    (1 row)
    
    
    sandbox=# show lc_numeric;
      lc_numeric
    ---------------
     polish_poland
    (1 row)
    
    
    sandbox=# SELECT TO_CHAR(76543210.98, '999G999G990D00');
         to_char
    -----------------
       76 543 210,98
    (1 row)
    

    我没有收到错误消息,但我收到了分隔符的垃圾。这可能是代码页问题吗?

    G格式字符串

    让我们想想这个。如果使用空格,则在网页上,该值可能在行尾或表格单元格的边界处拆分。我认为一个不间断的空间可能是一个更好的选择。

    UTF-8 Bit Distribution .)

    推荐文章