gpt4 book ai didi

postgresql - postgres 函数返回一个整数而不是数字

转载 作者:行者123 更新时间:2023-11-29 12:23:56 26 4
gpt4 key购买 nike

这个 postgres 函数应该返回一个十进制数,但结果却是一个整数。 SQL 本身可以正常工作。功能结构有什么问题?一个示例输入是文本中的度分秒经度 - '1012104.00E'

CREATE OR REPLACE FUNCTION getSEC(inORD VARCHAR)
RETURNS NUMERIC(10) AS $$
DECLARE
sec NUMERIC(10);
BEGIN
sec := cast(right(substring(inOrd,0,length(inOrd)-3,2)as numeric(10))/3600;
RETURN sec;
END; $$
LANGUAGE plpgsql;

提前致谢。

最佳答案

numeric 同时采用“精度”和“比例”。精度是总位数。比例是小数点右边有多少。 The Postgres docs provide an example .

The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point. So the number 23.5141 has a precision of 6 and a scale of 4. Integers can be considered to have a scale of zero.

如果您不提供比例,则默认为 0,这意味着您得到一个整数。因此 numeric(10) 是一个最多 10 位的整数。 Postgres 文档发现 SQL 标准的这个“特性”特别无用。

(The SQL standard requires a default scale of 0, i.e., coercion to integer precision. We find this a bit useless. If you're concerned about portability, always specify the precision and scale explicitly.)

如果你想要 1012104.00,你需要 numeric(9, 2)。 9 位数字,其中 2 位在小数点右边。

关于postgresql - postgres 函数返回一个整数而不是数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52084643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com