简体   繁体   中英

invalid input syntax for type numeric: “ ”

I'm getting this message in Redshift: invalid input syntax for type numeric: " " , even after trying to implement the advice found in SO.

I am trying to convert text to number.

In my inner join, I try to make sure that the text being processed is first converted to null when there is an empty string, like so:

nullif(trim(atl.original_pricev::text),'') as original_price

... I noticed from a related post on coalesce that you have to convert the value to text before you can try and nullif it.

Then in the outer join, I test to see that there's a limited set of acceptable characters and if this test is met I try to do the to_number conversion:

,case 
   when regexp_instr(trim(atl.original_price),'[^0-9.$,]')=0 
      then to_number(atl.original_price,'FM999999999D00') 
   else null 
 end as  original_price2

At this point I get the above error and unfortunately I can't see the details in datagrip to get the offending value.

So my questions are:

  1. I notice that there is an empty space in my error message:

invalid input syntax for type numeric: " " . Does this error have the exact same meaning as

invalid input syntax for type numeric:'' which is what I see in similar posts??

  1. Of course: what am I doing wrong?

Thanks!

It's hard to know for sure without some data and the complete code to try and reproduce the example, but as some have mentioned in the comments the most likely cause is the to_number() function you are using.

In the earlier code fragment you are converting original_price to text (string) and then substituting an empty string ('') if the value is NULL. Calling the to_number() function on an empty string will give you the error described.

Without the full SQL statement it's not clear why you're putting the nullif() function around the original_price in the "inner join" or how whether the CASE statement is really in an outer join clause or one of the columns returned by the query. However you could perhaps alter the nullif() to substitute a value that can be converted to a number eg '0.00' instead of ''.

Sorry I couldn't share real data. I spent the weekend testing small sets to try and trap the error. I found that the error was caused by the input string having no numbers, which is permitted by my regex filter:

when regexp_instr(trim(atl.original_price),'[^0-9.$,]') . 

I wrongly expected that a non numeric string like "$" would evaluate to NULL and then the to_number function would = NULL . But from experimenting it seems that it needs at least one number somewhere in the string. Otherwise it reduces the string argument to an empty string prior to running the to_number formatting and chokes.

For example select to_number(trim('$1'::text),'FM999999999999D00') will evaluate to 1 but select to_number(trim('$A'::text),'FM999999999999D00') will throw the empty string error.

My fix was to add an additional regex to my initial filter:

and regexp_instr(atl.original_price2,'[0-9]')>0 .  

This ensures that at least one number will be in the string and after that the empty string error went away.

Hope my learning experience helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM