Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql to_number() function format

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.

I've read about this function here and 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?

like image 986
0bj3ct Avatar asked Oct 28 '25 17:10

0bj3ct


2 Answers

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

select '00005469'::integer;

or using standard SQL:

select cast('00005469' as integer);

To be more safe if your column has decimals or not, use

 select  NULLIF('00005469', '')::decimal

This select '00005469'::integer; will not work if there are decimals

like image 29
Omari Victor Omosa Avatar answered Oct 31 '25 06:10

Omari Victor Omosa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!