Oracle To_Char函数如果它已经是一个字符串,该如何处理 [英] Oracle To_Char function How to handle if it's already a string

查看:173
本文介绍了Oracle To_Char函数如果它已经是一个字符串,该如何处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:我正在调用一个返回用户输入字段的函数。该字段通常会返回一个数字,例如120000,然后使用to_char转换为120,000。

问题:某些用户输入的值为120,000,这会在尝试使用to_char时给我一个错误。如果没有找到值,函数也会返回空格。我之前尝试过to_number,它有一个问题,我相信。问题:什么是解决这个问题的最好方法?案件陈述检查','?使用to_number然后to_char?

注意:我可以一起解决一个解决方案我只是想知道处理这个问题的最好方法是什么。

解决方案

与其使用REPLACE,您应该使用功能更强大的 REGEXP_REPLACE 函数。
http://www.orafaq.com/wiki/REGEXP_REPLACE



然后,您可以从字符串中删除任何非数字字符,然后将其格式化,然而您喜欢。



它会是这样的:

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

这会将所有非数字字符替换为null,从而有效地将其从字符串中移除。 b
$ b

也请看这个答案:
Oracle:替换字符串中的非数字字符


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.

解决方案

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

这篇关于Oracle To_Char函数如果它已经是一个字符串,该如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆