Postgresql to_number() 函数格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37294045/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:17:16  来源:igfitidea点击:

Postgresql to_number() function format

sqlpostgresqltype-conversionsqldatatypes

提问by 0bj3ct

I want to use postgresql to_number(numberString, format_mask). My numberString may contain leading zeros (I don't know the actual length of these zeros and the numberString in total). I want to trim these zeros and get the value as number.

我想使用 postgresql to_number(numberString, format_mask)。我的 numberString 可能包含前导零(我不知道这些零的实际长度和 numberString 的总数)。我想修剪这些零并将值作为数字。

I've read about this function hereand currently using the format:

我在这里阅读了有关此功能的信息,目前正在使用以下格式:

select to_number('00005469', '9999999999')

But if the length of '9's is less than the length of the numberString then I can't get the correct number. How can I make this work without writing a long list of '9' in format_mask?

但是如果 '9's 的长度小于 numberString 的长度,那么我就无法得到正确的数字。如果不在 format_mask 中写入一长串“9”,我该如何完成这项工作?

回答by a_horse_with_no_name

You don't need to_number()for this. Just cast the string to an integer:

你不需要to_number()这个。只需将字符串转换为整数:

select '00005469'::integer;

or using standard SQL:

或使用标准 SQL:

select cast('00005469' as integer);

回答by mentole

use like this it is simple:

像这样使用很简单:

to_number(to_char(yournumber, '99'),'99')

to_number(to_char(yournumber, '99'),'99')