Oracle To_Char function How to handle if it's already a string

我怕爱的太早我们不能终老 提交于 2019-12-10 19:26:02

问题


Scenario: I am calling a function that returns a field that the user enters in. The field usually returns a number like '120000' which I then use to_char to convert into '120,000'.

Problem: Some users enter in values such as '120,000' which gives me an error when trying to use to_char. Also the function will return a space ' ' if no value is found. I tried something with to_number earlier and it has a problem with the ' ' I believe.

Question: What would be the best way to handle this problem? Case statement checking for the ','? Using to_number then to_char?

Note: I can hack a solution together I'm just wondering what the best way to handle this is.


回答1:


Rather than using REPLACE you should use the more powerful REGEXP_REPLACE function. http://www.orafaq.com/wiki/REGEXP_REPLACE

You can then remove any non-numeric character from the string before then formatting it however you like.

In your case it would be something like:

REGEXP_REPLACE(<your field>, '[^0-9]+', '');

This replaces all non-numeric characters with null effectively removing them from the string.

See this answer too: Oracle: Replacing non-numeric chars in a string



来源:https://stackoverflow.com/questions/9069278/oracle-to-char-function-how-to-handle-if-its-already-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!