请帮助我如何在postgresql 45.000.00上以这种格式插入此值 我已经配置了一个colum数字10长度和2精度,但只保存值> 999.99
答案 0 :(得分:0)
数字存储没有任何“格式”。您只需将它们作为有效的SQL编号常量插入即可。
如果您希望使用特定格式,则在显示值时应用该格式:
create table jorge (n numeric(10,2));
insert into jorge values (4500000);
在SQL中,十进制值可能只包含一个.
标记小数。例如3.14
或123.42
45.000.00
是SQL中小数的无效常量(第二个点会出现语法错误:
postgres=> select 45.000.00;
ERROR: syntax error at or near ".00"
LINE 1: select 45.000.00;
^
postgres=>
要以您希望的方式显示,请使用to_char()
select to_char(n, '00G000G00')
from jorge;
现在使用的群组分隔符取决于您的lc_numeric
设置:
postgres=> set lc_numeric TO 'en';
SET
postgres=> select to_char(n, '00G000G00')
postgres-> from jorge;
to_char
------------
45,000,00
(1 row)
postgres=> set lc_numeric TO 'de';
SET
postgres=> select to_char(n, '00G000G00')
postgres-> from jorge;
to_char
------------
45.000.00
(1 row)
但是在应用程序中进行这种格式化更好 更好,而不是在SQL查询中。
答案 1 :(得分:0)
如果您的问题是将'45.000.00'
之类的输入字符串转换为numeric(10,2)
,那么解决方案就是使用正则表达式:
SELECT regexp_replace('45.000.00', '\.(?=[^.]{3})', '', 'g')::numeric(10,2);
┌────────────────┐
│ regexp_replace │
├────────────────┤
│ 45000.00 │
└────────────────┘
(1 row)