Usage [Reprinted] Oracle in TO_NUMBER () function

1 Introduction Usage
TO_NUMBER function () is one commonly used type Oracle conversion functions mainly to convert the string to numeric format, with the TO_CHAR () function returns the opposite.

To_number function has the following format:

To_number (VARCHAR2 or char, 'the format Model')
. 1
to_number function has a fixed number of predefined formats:

Value Meaning format
9 represents a number
0 0 forced display
$ dollar sign display
L forced display a local currency symbol
display a decimal point
, display a symbol thousands separator
2 some examples of
SQL> select to_number ( 'RMB234234.4350' , 'L999999 .0000 ') from Dual;
the tO_NUMBER (' RMB234234.4350 ',' L999999.0000
') --------------
234,234.435
. 1
2
. 3
. 4
the SQL> SELECT to_number (' $ 123,233,455,623.3400 ',' 999,999,999,999.0000 $ ') from Dual;
the TO_NUMBER (' $ 123,233,455,623.3400 ',' $ 999,999,999,999.0000
') --------------------
1.2323E +. 11
. 1
2
. 3
. 4
. 3 usage trap
Sometimes you will find that the use of TO_NUMBER () function and the syntax is correct, but Oracle has reported "invalid number" error, and you check in again in earnest again and determine the correct statement after screaming surprised that TO_NUMBER ( ) function may not know what the usage. In fact, this is probably the data you have requested is a problem, rather than SQL. Using the TO_NUMBER () function, they must make sure that the field is converted into a digital, such as a character string "20151008" is converted to a number 20151008, but the character string "2015-10-08" No. If your field contains the string "2015-10-08", and you also directly use the TO_NUMBER () function to operate, then it will report "invalid number" fault.

4 escape the trap
How to escape the trap?

1 front-end verification
as much as possible to make the necessary check at the time of user input, to ensure that the value of the input format is what we need.

2 background check
necessary to check the code in the background screening to the wrong value passed to the front desk and reasonably prompt.

3 SQL check
Once your values into the database, the problem becomes complicated. In another case, the data is historical, we can not modify it, but also make the necessary inquiries. This time we need to do something at the SQL level. Suppose you want to use the TO_NUMBER () function is varchar2 type field called "status". For example, if the illegal data are relatively long, you can add SQL check in length, that is where one more condition:

The LENGTH (Status) <= 10
. 1
or may be replaced ,, illegal character is also in a condition where the pay more:

The TO_NUMBER (the REGEXP_REPLACE (Status, '[^ 0-9]', ''))> 30
. 1
example the REGEXP_REPLACE () to replace the character "status" field, all non-numeric character is empty, and then again the TO_NUMBER () comparison can be friends!
---------------------
author: I am a dry fish hook
Source: CSDN
Original: https: //blog.csdn.net/dongdong9223/article/details/48972815
Disclaimer: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/AaronBear/p/11139376.html