To_number function

Hi,
i just stuck up with converting number to a character format.Please help.
my requirement is '234' should be displayed as "two hundredd thirty four".
Regards

user13490153 wrote:
Hi,
i just stuck up with converting number to a character format.Please help.
my requirement is '234' should be displayed as "two hundredd thirty four".
Regards
AskTom

Similar Messages

  • To_number function doesn't work with apex_collections in where clause

    One of my applications I have been using apex_collections and to_number function
    in the where clause. Since we upgraded apex to version 3.0.1 , it gives the invalid number error.
    Is this a bug in the new Version.
    I had to re-write the logic to fix the application.

    Can you show us that code? An example on apex.oracle.com where we can see the error would be great.
    Scott
    P.S. Please tell us your first name and put it into your handle and/or profile to help us.

  • How to override to_number function?!

    Hi,
    I already tried to find an answer by searching this forum but without success...
    My problem is, that I have a lot of mappings using the to_number() function to convert char into number. Now it turns out, that in some cases - due to the bad data qualtity - fields contain special chars e.g. 1234" Don't ask why ;) Of course the reguIar tu_number() function fails on that. Now I wrote a to_number function that handles the issue by removing all the special chars and then convert. I now want to override oracle's built-in to_number function with my own function.
    Do you have any ideas how that works? As a requirement the solution should be simple... It would be an imense work to edit every mapping. I global solution is preferred. Something like a priority option, to take self written functions first or sth...
    Thanks for any help!
    Cheers Steffen

    Hi,
    you could use OMB to change all occurance of that functino in your project.
    Unfortunately I know OWB allmost only from theory :-( but I remember, that you can set a context to a project or a mapping and all subsequent operations will happen within that horizont.
    As far as I saw, OMB isn't to hard to use, even without days of learning, just by looking for fitting operation, but I know documentation about OMB is pretty poor, almost only syntax reference is available.
    really overriding the pre_build to_number-function might be possible as sys, but I would avoid that or only do in a relly play-instance. I suppose you tried allready simply to run 'create or replace ...' ?
    Thanks for your tip about number checking, but I tried allready many variations functions like that. I never can get it validated:
    I have a function is_number_num, returning 1 for ok, 0 for false, which si working fine on SQL*PLUS, but when I use it splitter-condition, in this variations:
    "IS_NUMBER_NUM"( GRP_IN.ANZAHL,'NLS_NUMERIC_CHARACTERS='',.''') = 1
    I get: PLS-00801 internal error [phdxcsql_canonicalize_sql:state]
    with .... == 1
    I get:
    Line 0, Col 0:
    The expression is not properly formed.
    I have no bloody idea, what that means and what might be wrong in here ? Even google does not return 1 singel page for the error-parameter !
    I tried several versions:
    "IS_NUMBER_NUM"( GRP_IN.ANZAHL,'NLS_NUMERIC_CHARACTERS='',.''') = 1
    also this: "IS_NUMBER_NUM"( GRP_IN.ANZAHL) = 1
    but nothing is accepted !
    Also I made a more easy function for checking for date-format, which works fine in SQL*PLUS, but can't be validated in OWB-splitter.
    Oracle support is playing for time since days !
    thsi function brings same error, depending if I use 1 or 2 "=" !???
    any idea ?
    somehow I have the feeling, I missed 1 easy rule on how to use a splitter-condition correctly ?
    But I can't see anything wrong in my constructions, especially because they are working fine in SQL*PLUS !???
    thanks for any hint, LaoDe

  • To_Number function is changing decimal places????

    Hi all..I have a number 11,308,861.09
    To sum up with another..I have to use to_number function
    When I am using this function, I am viewing that as 1.13088610859
    Please help....

    Okay.....I tried with seperate sample xml after reading you post....
    This number looked good.
    But in the original template this still showed the same way with incoreect decimals.
    then.. I tried this:
    xdoxslt:to_number(number(ELEMENT))
    This worked.

  • TO_NUMBER Function Issue

    Hi guys,
    would like to ask on what cases would a query return an invalid number error? I know if the column has special characters and letters it would return an invalid number when you use to_number function but I currently have a scenario wherein I have whole numbers in a varchar column but i still get invalid number error.
    the data that i'm trying to convert to number was from a csv file. When i first loaded the csv data into the oracle external table , it returned an invalid number error. So, what i did was change the data type of the external table into varchar so that it will load the data.
    Now what i want to do is transfer the data from external table to a dimensional table. Since the data type in dimensional table is Number , i have to use to_number function to be able to insert the data. I already tried to use to_number(to_char(trim(columnA))) but still wouldn't work.
    Any idea? Appreciate your help.
    Thanks.

    977487 wrote:
    I checked the csv file through unix and found that there is a control M in Col2. Believe that's what causes the invalid number error.
    You guys are awesome. Thank you very much!Was this data - the .csv file - moved between a *nix and a Windows platform?  If so, which direction?
    *nix and Windows use a different standard to indicate "end of record".  When moving text files between platforms, you have to take that into account.  If moving the file with ftp, you have to indicate it is a 'text' or 'ascii' transfer.  If you forget, then after the fact you can use the 'dos2unix' or 'unix2dos' utility to fix the file.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • To_number function return Invalid number

    Dear All,
    We have problem recently regarding one of our query that using to_number function. It worked smoothly for 4-5 years until recently our client complaints that the query is returning error ORA-01722: invalid number.
    This is the SQL Script -> SELECT * FROM <table_name> WHERE to_number(b) between :param1 and :param2;
    * Column b is defined as VARCHAR2(20)
    First thing that come to my mind is this error is due to invalid value entered by user. Using the solutions provided by users in this forum and internet, I created SQL a function that checks whether the value is numeric or not.
    So, when I run the query (+SELECT b from <table_name> WHERE is_numeric(b) = 0 AND ROWNUM < 20+) this is the result that I get.
    b
    251567
    251568
    251569
    251570
    251571
    (Up to 11 record; value from 251567 - 251577)
    Is there any limitation on the maximum value that can be converted using to_number* function?*
    I have search the solutions over the internet and I accept the solution/recommendation that we should not store numeric value on varchar/char column; but I hope somebody can explain why I can't use to_number to convert the above figures to numeric, because when I execute SELECT to_number('251567') FROM DUAL it doesn't returned any error)
    Thank you in advance for any help or clarification on this issue.
    Edited by: user5535734 on Apr 16, 2012 10:46 PM

    Funny answer, Billy!
    Sounds like: The world is evil, so you have 3 choices:
    1. Implement a good world
    2. Be a bad guy, too
    3. Live with that bad world and its consequences
    I guess, 930427 only asked for some protection...
    If one has to deal with two external system to move data from one to another - you can't change either of these systems (1 or 2) and you can't afford to fail (3).
    Sometimes blanks inside the numeric values are the reason for INVALID NUMBER, i.e. "123 456".
    as simple
    to_number(replace(VALUE_CHAR, ' ', NULL))
    may help...

  • TO_Number Function showing wrong values

    Hi All,
              I am passing a Char value from oracle forms to oracle reports as parameter.
    In Report Query I'm converting the char value into number for comparison using TO_NUMBER() function. As the char value is large value like '001002004006004' it is converting to something like 1.2e+12 and my query results goes wrong. I want value, something like 1002004006004( as initial 00 will be removed).
    I'm using Oracle forms 10g and Reports 10g
    Can anyone please help me.
    Thanks
    Sandeep
    ----Don't think outside the box, Think as if there is no Box at all.

    Hi, Sandeep,
    Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements) that shows what the problem is, and the correct results you want from that sample data.  Explain how you get those results from that data.
    See the forum FAQ: https://forums.oracle.com/message/9362002#9362002
    Don't confuse how a number is displayed with what that number is.  SQL*Plus (and other front ends), by default, allows only 10 character spaces for displaying NUMBERs.  If you have a NUMBER that doesn't fit into 10 spaces, then SQL*Plus will round the digits after the decimal point.  If it still doesn't fit (which is the case with 1002004006004) then SQL*Plus will use scientific notation, rounding the decimal places.  The actual value (at least 38 significant digits) will be used in all computations, regardless of how the NUMBER is displayed.  I doubt if what you posted has anything to do with the real problem.

  • TO_NUMBER function on varchar2 column with numbers and strings

    I need to create a column in a view that converts a varchar2 column data to number. The problem is that some
    of the data have strings and some numbers. I get "ORA-01722: invalid number" error when Oracle tries to covert
    strings (e.g. 'ABCD') to number. What I need is to get a NULL value if the data is an invalid number.
    How can I rewrite the following to get NULL value is a string is invalid number?
    select to_number('abcd') numberColumn
    from dual
    thanx
    Alfred

    SQL> select * from test_char_num;
    ALPHA_NUM
    ABC
    DEF
    123
    234
    A12
    SQL> select decode(NVL(length(trim(translate(alpha_num, '1234567890', ' '))), 0),
      2                0, alpha_num, 'NULL') new_alpha_num
      3  from test_char_num;
    NEW_ALPHA_
    NULL
    NULL
    123
    234
    NULLP.S I am printing 'NULL' just to show the result, Please replace the 'NULL string with NULL value in SQL.
    Thx,
    Sri

  • To_number function and the "invalid number" error

    Dear all,
    I have searched the forum for threads relating to the ORA-01722: invalid number error, could not find the answer I am looking for.
    What I was trying to do was
    select * from table1 where to_number(field1) > 1000
    field1 is a varchar2 data type.
    I tried all sorts of things i.e using fmt, nls params as defined in the documentation, nothing worked.
    Though the practical problem was solved by
    select * from table1 where field1 > '1000'
    I would still like to know why this error occurs. Can someone help ?
    Regards
    Crusoe

    I think the database engine should simply return the rows that successfully convert to number and meet the where condition Oracle does not work in that way ;)
    Then try this...
    Just you need to add the below where clause to your source query. as this will retrieve only the number, to_number should work without any problem.
    But still you have to create a subquery to escape the invalid number error.
    PRAZY@11gR1> select * from tablea;
    FIELD1
    123
    123.345
    45
    AVC
    23.234.234
    ABC.234
    345.45
    7 rows selected.
    Elapsed: 00:00:00.00
    PRAZY@11gR1> select to_number(field1) from tablea where  rtrim(trim(regexp_replace(field1,'\.','',1,1)),'0123456789') is null;
    TO_NUMBER(FIELD1)
                  123
              123.345
                   45
               345.45
    4 rows selected.
    Elapsed: 00:00:00.01Regards,
    Prazy
    Edited by: Prazy on Mar 23, 2010 4:00 PM

  • Varchar to Number with TO_NUMBER() function!

    Hello,
    Im trying to convert a string to a number, i know it must be very simple but i cant figure out the right syntax.
    im trying it like this:
    matchums.PROV_GRUNDLAGE:= TO_NUMBER(rechnungsaus.PROV_GRUNDLAGE,10.2);
    -- rechnungsaus.PROV_GRUNDLAGE is a varchar2(30)
    -- matchums.PROV_GRUNDLAGE is number(10,2)
    How do i convert this string the easiest way??
    thanks for help!
    Phil

    Oh, sorry.
    matchums.PROV_GRUNDLAGE is number.
    rechnungsaus.PROV_GRUNDLAGE is varchar.
    Only this!
    matchums.PROV_GRUNDLAGE := to_number(rechnungsaus.PROV_GRUNDLAGE);
    SQL> alter session set nls_territory=Germany;
    Session altered.
    SQL> select to_number('12345678,91') from dual;
    TO_NUMBER('12345678,91')
    12345678,9
    SQL> set numwidth 20
    SQL> select to_number('12345678,91') from dual;
    TO_NUMBER('12345678,91')
    12345678,91
    ---- Additional Info.
    matchums.PROV_GRUNDLAGE
    := to_number(rechnungsaus.PROV_GRUNDLAGE,,'99999999D99','nls_numeric_characters = '',.''');
    SQL> select to_number('12345678,91','99999999D99','nls_numeric_characters = '',.''') from dual;
    TO_NUMBER('12345678,91','99999999D99','NLS_NUMERIC_CHARACTERS='',.''')
                                                               12345678,91Message was edited by:
    ushitaki

  • Issue with to_number

    Hello all.
    Please explain me the following phenomena, i cant figure this out.
    Why is 'to_number' function forced by the optimizer in this query @ the operation number 11 ?
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for Linux: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    SQL>
    SQL>
    SQL>
    SQL> explain plan for
      2  with q as (
      3    select v$parameter.value bs_value
      4    from v$parameter
      5    where name='db_block_size'
      6  )
      7  select a.username,
      8         a.sid, a.serial#,
      9         round(((b.blocks*bs_value)/1024/1024),2) size_mb,
    10         a.status,
    11         b.segfile#,
    12         b.segblk#
    13  from v$session a,
    14       v$sort_usage b,
    15       v$process c,
    16       q
    17  where a.saddr = b.session_addr
    18    and a.paddr = c.addr
    19    and ((nvl(b.blocks,0)*nvl(bs_value,0))/1024/1024) > 0
    20  -- group by b.segfile#, b.segblk#,
    21  order by size_mb;
    Explained.
    SQL>
    SQL>
    SQL> set linesize 250
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 3927520946
    | Id  | Operation                    | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT             |                 |     1 |   668 |     3 (100)| 00:00:01 |
    |   1 |  SORT ORDER BY               |                 |     1 |   668 |     3 (100)| 00:00:01 |
    |   2 |   NESTED LOOPS               |                 |     1 |   668 |     2 (100)| 00:00:01 |
    |*  3 |    HASH JOIN                 |                 |     1 |   655 |     2 (100)| 00:00:01 |
    |*  4 |     HASH JOIN                |                 |     1 |   625 |     2 (100)| 00:00:01 |
    |   5 |      NESTED LOOPS            |                 |     1 |   509 |     1 (100)| 00:00:01 |
    PLAN_TABLE_OUTPUT
    |*  6 |       HASH JOIN              |                 |     1 |   496 |     1 (100)| 00:00:01 |
    |   7 |        NESTED LOOPS          |                 |     1 |   428 |     1 (100)| 00:00:01 |
    |*  8 |         HASH JOIN            |                 |     1 |   138 |     1 (100)| 00:00:01 |
    |*  9 |          FIXED TABLE FULL    | X$KSUSE         |     1 |    69 |     0   (0)| 00:00:01 |
    |* 10 |          FIXED TABLE FULL    | X$KTSSO         |     1 |    69 |     0   (0)| 00:00:01 |
    |* 11 |         FIXED TABLE FULL     | X$KSPPCV        |     5 |  1450 |     0   (0)| 00:00:01 |
    |* 12 |        FIXED TABLE FULL      | X$KSPPI         |     1 |    68 |     0   (0)| 00:00:01 |
    |* 13 |       FIXED TABLE FIXED INDEX| X$KSLED (ind:2) |     1 |    13 |     0   (0)| 00:00:01 |
    |* 14 |      FIXED TABLE FULL        | X$KSUSE         |     1 |   116 |     0   (0)| 00:00:01 |
    |* 15 |     FIXED TABLE FULL         | X$KSUPR         |     1 |    30 |     0   (0)| 00:00:01 |
    |* 16 |    FIXED TABLE FIXED INDEX   | X$KSLED (ind:2) |     1 |    13 |     0   (0)| 00:00:01 |
    PLAN_TABLE_OUTPUT
    Predicate Information (identified by operation id):
       3 - access("S"."KSUSEPRO"="ADDR")
       4 - access("S"."ADDR"="KTSSOSES")
       6 - access("X"."INDX"="Y"."INDX")
           filter(TRANSLATE("KSPPINM",'_','#') NOT LIKE '#%' OR "KSPPSTDF"='FALSE' OR
                  BITAND("KSPPSTVF",5)>0)
       8 - access("KTSSOSES"="S"."ADDR" AND "KTSSOSNO"="S"."KSUSESER")
    PLAN_TABLE_OUTPUT
       9 - filter("S"."INST_ID"=USERENV('INSTANCE') AND BITAND("S"."KSSPAFLG",1)<>0 AND
                  BITAND("S"."KSUSEFLG",1)<>0)
      10 - filter("X$KTSSO"."INST_ID"=USERENV('INSTANCE'))
      11 - filter(NVL("KTSSOBLKS",0)*TO_NUMBER(NVL("KSPPSTVL",'0'))/1024/1024>0)
      12 - filter("KSPPINM"='db_block_size' AND "X"."INST_ID"=USERENV('INSTANCE') AND
                  TRANSLATE("KSPPINM",'_','#') NOT LIKE '##%')
      13 - filter("S"."KSUSEOPC"="E"."INDX")
      14 - filter("S"."INST_ID"=USERENV('INSTANCE') AND BITAND("S"."KSSPAFLG",1)<>0 AND
                  BITAND("S"."KSUSEFLG",1)<>0)
      15 - filter("INST_ID"=USERENV('INSTANCE') AND BITAND("KSSPAFLG",1)<>0)
      16 - filter("S"."KSUSEOPC"="E"."INDX")
    44 rows selected.

    Actually I can reproduce, when hardcoding block size the query runs ok, but the other version keeps on ending up in an error:
    SQL>   select a.username,
      2           a.sid, a.serial#,
      3           (b.blocks * 8192)/1024/1024 size_mb,
      4           a.status,
      5           b.segfile#,
      6           b.segblk#
      7    from v$session a,
      8         v$sort_usage b,
      9         v$process c
    10    where a.saddr = b.session_addr
    11      and a.paddr = c.addr
    12      and (b.blocks * 8192)/1024/1024 > 0;
    USERNAME                              SID    SERIAL#    SIZE_MB STATUS     SEGFILE#    SEGBLK#
    *********                             254       6958          1 INACTIVE        201      33280
    *********                             254       6958          1 INACTIVE        201      33664
    2 rows selected.
    SQL>  with q as (
      2    select v$parameter.value bs_value
      3    from v$parameter
      4    where name='db_block_size'
      5  )
      6  select a.username,
      7         a.sid, a.serial#,
      8         round(((b.blocks*bs_value)/1024/1024),2) size_mb,
      9         a.status,
    10         b.segfile#,
    11         b.segblk#
    12  from v$session a,
    13       v$sort_usage b,
    14       v$process c,
    15       q
    16  where a.saddr = b.session_addr
    17    and a.paddr = c.addr
    18    and ((nvl(b.blocks,0)*nvl(bs_value,0))/1024/1024) > 0;
      and ((nvl(b.blocks,0)*nvl(bs_value,0))/1024/1024) > 0
    ERROR at line 18:
    ORA-01722: invalid number
    SQL> select * from v$version
      2  /
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE    11.1.0.7.0      ProductionNow I've been fiddling with the query, adding to_number, omitting nvl etc. etc., but I keep on getting invalid number,. no matter what...
    This is either very weird or I'm missing something very obvious here.. ?:|

  • Need help in Performance tuning for function...

    Hi all,
    I am using the below algorithm for calculating the Luhn Alogorithm to calculate the 15th luhn digit for an IMEI (Phone Sim Card).
    But the below function is taking about 6 min for 5 million records. I had 170 million records in a table want to calculate the luhn digit for all of them which might take up to 4-5 hours.Please help me performance tuning (better way or better logic for luhn calculation) to the below function.
    A wikipedia link is provided for the luhn algorithm below
    Create or Replace FUNCTION AddLuhnToIMEI (LuhnPrimitive VARCHAR2)
          RETURN VARCHAR2
       AS
          Index_no     NUMBER (2) := LENGTH (LuhnPrimitive);
          Multiplier   NUMBER (1) := 2;
          Total_Sum    NUMBER (4) := 0;
          Plus         NUMBER (2);
          ReturnLuhn   VARCHAR2 (25);
       BEGIN
          WHILE Index_no >= 1
          LOOP
             Plus       := Multiplier * (TO_NUMBER (SUBSTR (LuhnPrimitive, Index_no, 1)));
             Multiplier := (3 - Multiplier);
             Total_Sum  := Total_Sum + TO_NUMBER (TRUNC ( (Plus / 10))) + MOD (Plus, 10);
             Index_no   := Index_no - 1;
          END LOOP;
          ReturnLuhn := LuhnPrimitive || CASE
                                             WHEN MOD (Total_Sum, 10) = 0 THEN '0'
                                             ELSE TO_CHAR (10 - MOD (Total_Sum, 10))
                                         END;
          RETURN ReturnLuhn;
       EXCEPTION
          WHEN OTHERS
          THEN
             RETURN (LuhnPrimitive);
       END AddLuhnToIMEI;
    http://en.wikipedia.org/wiki/Luhn_algorithmAny sort of help is much appreciated....
    Thanks
    Rede

    There is a not needed to_number function in it. TRUNC will already return a number.
    Also the MOD function can be avoided at some steps. Since multiplying by 2 will never be higher then 18 you can speed up the calculation with this.
    create or replace
    FUNCTION AddLuhnToIMEI_fast (LuhnPrimitive VARCHAR2)
          RETURN VARCHAR2
       AS
          Index_no     pls_Integer;
          Multiplier   pls_Integer := 2;
          Total_Sum    pls_Integer := 0;
          Plus         pls_Integer;
          rest         pls_integer;
          ReturnLuhn   VARCHAR2 (25);
       BEGIN
          for Index_no in reverse 1..LENGTH (LuhnPrimitive) LOOP
             Plus       := Multiplier * TO_NUMBER (SUBSTR (LuhnPrimitive, Index_no, 1));
             Multiplier := 3 - Multiplier;
             if Plus < 10 then
                Total_Sum  := Total_Sum + Plus ;
             else
                Total_Sum  := Total_Sum + Plus - 9;
             end if;  
          END LOOP;
          rest := MOD (Total_Sum, 10);
          ReturnLuhn := LuhnPrimitive || CASE WHEN rest = 0 THEN '0' ELSE TO_CHAR (10 - rest) END;
          RETURN ReturnLuhn;
       END AddLuhnToIMEI_fast;
    /My tests gave an improvement for about 40%.
    The next step to try could be to use native complilation on this function. This can give an additional big boost.
    Edited by: Sven W. on Mar 9, 2011 8:11 PM

  • To_number conversion error in OWB runtime

    I have a mapping in OWB 10.1.0.5 that does something like that :
    select t1.a_field,to_number(t2.a_stringField)
    from a_table t1
    an_other_table@source_schemas@source_database t2
    where (....)
    the t2.a_stringField may contain:
    '1256'
    '5'
    '12'
    '5,8'
    when I run my mapping from SQL_PLUS
    begin
    my_mapping.main();
    end;
    it works well,
    but when I run my mapping with OWB, my mapping terminates with errors on the line containing a decimal value (the line with coma decimal separator).
    I deployed my mapping over three similar database (developpement,test,production) and the problem appends for just one of them !
    any idea ?
    these are my database parameter :
    NLS_TERRITORY     AMERICA
    NLS_NUMERIC_CHARACTERS '.,'
    the paramters for the current user (target schemas):
    NLS_TERRITORY     FRANCE
    NLS_NUMERIC_CHARACTERS ',.'
    my 'source_database' parameters :
    NLS_TERRITORY     AMERICA
    NLS_NUMERIC_CHARACTERS '.,'
    and the parameters for the user 'source_schemas' :
    NLS_TERRITORY     FRANCE
    NLS_NUMERIC_CHARACTERS ',.'

    Hi Jean,
    I think you need to change NLS_LANG to the same value as on your other two servers (and don't forget to restart OWB runtime).
    Or you can specify nls parameter in TO_NUMBER function:
    TO_NUMBER('9,5','999D99', 'NLS_NUMERIC_CHARACTERS = '', ''')
    Regards,
    Oleg

  • Optimize function to correct a string to be converted to number

    Hi,
    I have imported almost 6.5 milion rows into a table (using sql loader) from a flat file (which contains the NUL caracter (ASCII 0) - not space, not NULL). To make the load easier I set the datatype as VARCHAR2 for all columns. For columns with string data I used a TRIM(REPLACE(field,' ','')) to get rid of the NUL and it works.
    But for the column that must be converted to numeric I am trying to use to_number () function and it fails because the numeric data is mixed with other characters.
    To solve this problem I created this function :
    CREATE OR REPLACE
    FUNCTION string_to_number
    (p_string_source IN VARCHAR2 ) RETURN VARCHAR2 IS v_output_string varchar2(150);
    -- This function takes a string as parameter and has 2 outputs:
    -- 1. If the source string cannot be converted to number throw 'Error' as the output message to identify the line with the issue
    -- 2. A string that can be converted successfully as Number;
    -- The necessity of this function came up after an import from a flat file where the resulted string contained strange characters shown as spaces
    --check if '-' is exists and is on the first position or if in the source string exists more than one '.'
    -- ASCII codes accepted in the string :
    -- 45 '-' ; 46 '.' ; from 48 (0) to 57 (9)
    BEGIN
    DECLARE v_minus VARCHAR2(1);
    v_dot INTEGER;
    BEGIN
    SELECT substr(p_string_source,1,1) INTO v_minus FROM dual;
    SELECT instr(p_string_source,'.',1,2) INTO v_dot FROM dual;
    --check if '-' is exists and is on the first position or if in the source string exists more than one '.'
    -- ASCII codes accepted in the string :
    -- 45 '-' ; 46 '.' ; from 48 (0) to 57 (9)
    IF v_minus NOT IN ('-','.','0','1','2','3','4','5','6','7','8','9')
    -- or there are two dots '.' in the string
    OR v_dot <> 0
    THEN v_output_string := 'Error';
    ELSE
    BEGIN
    -- for every character of the string we'll check if it's a number to add it to the outcome string;
    -- if it's not an accepted character it will be ignored
    DECLARE v_length_source int := length(p_string_source);
    v_counter int :=1;
    v_add_in_number VARCHAR2(1);
    BEGIN
    FOR v_counter IN 1..v_length_source LOOP
    BEGIN
    SELECT SUBSTR(p_string_source,v_counter,1) into v_add_in_number from dual;
    IF v_add_in_number IN ('-','.','0','1','2','3','4','5','6','7','8','9')
    THEN v_output_string := v_output_string ||v_add_in_number;
    END IF;
    END;
    END LOOP;
    END;
    -- in case the string is in format '.00034' we'll add a 0 in front of the string to be accepted as argument by TO_NUMBER function
    IF v_minus = '.'
    THEN v_output_string := '0'||v_output_string;
    ELSE
    BEGIN
    v_output_string := v_output_string;
    END;
    END IF;
    END;
    END IF;
    END;
    RETURN v_output_string;
    END;
    The main idea is to check every string (the parameter will be the value from the Amount column) for permitted characters that compose a numeric value:
    1. To begins with numeric, '-' or '.'
    2. To have only one '.' (as a decimal separator);
    3. To compare every character of the string with the permitted ones - the non-compliant will be rejected
    This way the resulting string (v_output_string) will be successfully converted to number
    I admit that I don't have much experience using PL/SQL that is why I am asking your help to optimize this function to improve its performance. Could you help me on this, please ?
    TIA,
    JohnP
    Edited by: petresion on 04-Oct-2012 01:33

    Perform a function here on all 6.5 million of rows will never be efficient.
    I would modify Peter's approach a little bit,
    1. Load directly into staging t1 without any checking (or create an external table)
    2. Transfer from t1 to a list partitioned t2 with list values in ('yes', 'no','null','other')
    --simple check using translate()
    3. Apply your special function only on rows in the 'other' partition (hopefully much fewer rows left)
    --other checks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • To_number() with format pattern and nvarchar2

    Hi,
    I've had a problem with converting a number in US format in to a number. Due to the settings of our Oracle we have German locale settings. Thus I use the NLS format parameters in the to_number function. It works pretty well if I set the number string directly into the function call. However it did not work a the column of type nvarchar2 (which is actually permitted according to the documentation). This simple statement shows the problem:
    alter session set NLS_NUMERIC_CHARACTERS = ',.';
    DECLARE
    evalue NVARCHAR2(20);
    evalue2 NUMBER(25, 15);
    BEGIN
    SELECT '5.124' INTO evalue FROM dual;
    SELECT to_number(evalue,
    '9G990D9999999999999999999',
    'NLS_NUMERIC_CHARACTERS = ''.,''') s
    INTO evalue2
    FROM dual;
    dbms_output.put_line(evalue2);
    END;
    It gives me an ORA-01722 invalid number. It can be fixed by changing the column to e.g. VARCHAR(20) or just use an cast before passing to the to_number()
    DECLARE
    evalue NVARCHAR2(20);
    evalue2 NUMBER(25, 15);
    BEGIN
    SELECT '5.1234' INTO evalue FROM dual;
    SELECT to_number(cast(evalue as varchar2(20)),
    '9G990D9999999999999999999',
    'NLS_NUMERIC_CHARACTERS = ''.,''') s
    INTO evalue2
    FROM dual;
    dbms_output.put_line(evalue2);
    END;
    I played around with the number and found out that the specified NLS_NUMERIC_CHARACTERS is not applied when the column type is NVARCHAR2 and is still trying to convert it using the session's locale settings.
    Does anybody know if this is fixed already in a later version?
    System: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit
    Kind regards,
    Mario

    Hi,
    It seems to work if you first make it a char before the to_number :DECLARE
    evalue NVARCHAR2(20);
    evalue2 NUMBER(25, 15);
    BEGIN
    SELECT '5.124' INTO evalue FROM dual;
    SELECT to_number(to_char(evalue),          <--- to_char added here
    '9G990D9999999999999999999',
    'NLS_NUMERIC_CHARACTERS = ''.,''') s
    INTO evalue2
    FROM dual;
    dbms_output.put_line(evalue2);
    END;
    /

Maybe you are looking for

  • I can't sync an album(imported from a CD)on my itunes library to my iphone 5?

    I just bought a new cd, imported it successfully onto my itunes library so I am able to play the songs on my windows 7 netbook but I just cant sync them to my iphone5, when I connect the phone to my laptop and click on the music it shows the album th

  • Custom BIOS for GT70ond

    Hi! Mainboard:  MS-1762 BIOS Version: E1762IMS.10U BIOS Date:  1/21/2013 EC-FW Version:  E1762EMS1 verT.17 Donation Confirm number:7PG81025P96222603

  • Downloading of java 2 SDk, version 1.4.1_01

    I am trying to download the subject version , but whenever I click on the file j2sdk-1_4_1_01-windows-i586.exe . after some time, message appearthat Gateway time pass.I am a student at York university Toronto.I need this version to do my labs at home

  • JEditorPane and HyperlinkListener Problems

    Here's my code: public class WizardHelpScreen extends JFrame implements HyperlinkListener { private Image theIcon = new ImageIcon(getClass().getResource("icon.gif")).getImage(); private JEditorPane helpPane; private JScrollPane helpScrollPane; public

  • Calendar problems with new 4.5

    I updated my device yesterday with 4.5. Now my calendar only syncs one way, which is from Outlook to my device. In addition, I use the "today" theme on my phone so I can see my daily appointments on the home screen. Now the only appointments that sho