在 postgresql 中将浮点数解析为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28680560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse float to int in postgresql
提问by HCarrasko
I've a set of data in a postgresql DB, where one of these columns store string data in float format, but now I need remove the decimal component of the string. How can I do this using an sql updatestatement in my BD console? Is that possible?
我在 postgresql 数据库中有一组数据,其中一个列以浮点格式存储字符串数据,但现在我需要删除字符串的十进制部分。如何在 BD 控制台中使用sql update语句执行此操作?那可能吗?
for example:
例如:
"25.3" -> "25"
If it does not possible how can I do this? Thanks in advance.
如果不可能,我该怎么做?提前致谢。
回答by Lucas
Surely you would be better suited casting the text columns, to numeric to integer, so that rounding is taken into consideration e.g.
当然,您更适合将文本列转换为数字到整数,以便考虑舍入,例如
SELECT '25.3'::numeric::integer AS num1, '25.5'::numeric::integer AS num2
SELECT '25.3'::numeric::integer AS num1, '25.5'::numeric::integer AS num2
which would return integers of 25 and 26 respectively.
这将分别返回 25 和 26 的整数。
If you were not concerned with the digits following the point, the floor()::integer function or a substring, as mentioned should be fine.
如果您不关心点后面的数字,那么 floor()::integer 函数或子字符串,如上所述应该没问题。
回答by Raj Srivastava
Since it is a string, you can use string functions to drop the after decimal digits. If you do not want to round them off, just drop the decimal part then use -
由于它是一个字符串,您可以使用字符串函数来删除小数点后的数字。如果您不想将它们四舍五入,只需删除小数部分然后使用 -
update table_name
set column_name = substring(column_name from 1 for position('.' in column_name)-1);
If you want rounding off, then you can use the cast as mentioned by @mlinth.
如果你想四舍五入,那么你可以使用@mlinth 提到的演员表。