`

Converting text to numbers

 
阅读更多
A table is created ...
create table safe_to_number_t (
  txt varchar2(10)
);
... and filled with text of which some can be converted to numbers:
insert into safe_to_number_t values ('1.0'   );
insert into safe_to_number_t values ('1'     );
insert into safe_to_number_t values ('22.9'  );
insert into safe_to_number_t values ('FOO'   );
insert into safe_to_number_t values ('-8.99' );
insert into safe_to_number_t values ('-.22'  );
insert into safe_to_number_t values ('-BAR'  );
insert into safe_to_number_t values ('23-45' );
However, some txts cannot be converted...
select to_number(txt) from safe_to_number_t;
... and an error is thrown:
ERROR:
ORA-01722: invalid number
So, a function is created that can safely convert strings to numbers. When the string is inconvertable, it returns null:
create function safe_to_number(txt in varchar2) return number is 
begin
  return to_number(txt);
exception when value_error then
  return null; 
end safe_to_number;
/
select safe_to_number(txt), txt from safe_to_number_t;
SAFE_TO_NUMBER(TXT) TXT
------------------- ----------
                  1 1.0
                  1 1
               22.9 22.9
                    FOO
              -8.99 -8.99
               -.22 -.22
                    -BAR
                    23-45
select safe_to_number(txt), txt from safe_to_number_t;

With regexp_substr

In 10g, it's possible to use regexp_substr for the same effect:
select to_number(
  regexp_substr(txt,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')
) the_number,
  txt
from safe_to_number_t;

分享到:
评论

相关推荐

    A Smarter Way to Learn JavaScript.pdf

    书中讲解了如何对数字进行四舍五入(Rounding numbers)、生成随机数(Generating random numbers)、将字符串转换为整数和小数(Converting ...)、以及将数字转换为字符串(Converting numbers to strings)...

    强大的免费的十六进制编辑器

    - Easy converting of text to hex string in dialogs (e.g. "abc" -> "61 62 63") - Decoding and encoding of 1, 2, 4, and 8 byte integers or 4/8 byte floats in 2 possible byte orders - Bit manipulation ...

    A samart way to learn javascript

    27. Converting strings to numbers, numbers to strings(字符串和数字的互相转换):了解不同数据类型之间的转换方法。 28. Controlling the length of decimals(控制小数点长度):通过四舍五入、向下取整等方式...

    WPTools.v6.29.1.Pro

    - change in DBWPRich.pas to use LoadFromString instead of Text - fix possible range check error - change in WPTbar.pas to use different default for BevelOuter - change in WPIOHTML to use default ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The PointOnLine() function calulations have "single" type numbers overflow problem (changed to "double"). - FIX: The pfJoin and pfClose flags incorrectly calculates in GetEditPathCaps(). ...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    - Converting numbers to strings. - Joining strings using the `join()` method. 29. **Add Numbers** - **Objective:** Add a list of numbers. - **Key Concepts:** - Using loops to iterate over ...

    Get Programming with Go

    Converting Between Types Chapter 6. Capstone: The Vigenère Cipher Unit 3 - BUILDING BLOCKS Chapter 1. Functions Chapter 2. Methods Chapter 3. First-Class Functions Chapter 4. Capstone: Temperature ...

    Python Cookbook, 2nd Edition

    Converting HTML Documents to Texton a Unix Terminal Chapter 2. Files Introduction Recipe 2.1. Reading from a File Recipe 2.2. Writing to a File Recipe 2.3. Searching and Replacing Text in a...

    VB编程资源大全(英文源码 其它)

    It can be added to the 'Tools' menu in DevStudio."<END><br>53,convertbase.zip VB has built in functions for converting from decimal to Hex and Octal, but nothing for converting from Octal and Hex ...

    Python 2.6 Graphics Cookbook.pdf

    Chapter 3, Handling Text: This chapter demonstrates how to control font size, color, and position using any of the font typefaces installed on the specific operating system being used. A simple ...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + added a property TruncateLongTexts to the XLS OLE export that allows to disable truncating texts longer than a specified limit + added option EmbedProt which allows to disable embedding fonts into ...

    C Programming

    - **From Problem to Program**: This discusses the process of converting a problem statement into a program, including understanding the problem, designing an algorithm, coding, testing, and debugging....

    BobBuilder_app

    Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr....Add to your CodeProject bookmarks ...In v2.3 a single simple change of converting internal classes ...

    R for beginner

    - **Random Sequences**: Functions like `rnorm()`, `runif()`, etc., can be used to generate random numbers following specific distributions. ##### Manipulating Objects R offers a wide range of ...

    IOS5 Programming Cookbook

    - **Adding Controls**: In Interface Builder, you can drag and drop various UI elements such as buttons, labels, and text fields onto your view controller. You can also customize their appearance and ...

    python3.6.5参考手册 chm

    Text Vs. Data Instead Of Unicode Vs. 8-bit Overview Of Syntax Changes New Syntax Changed Syntax Removed Syntax Changes Already Present In Python 2.6 Library Changes PEP 3101: A New Approach To ...

    The Art of Assembly Language Programming

    3 Organization of This Text and Pedagogical Concerns 4 Obtaining Program Source Listings and Other Materials in This Text <br>Section One: Machine Organization <br>Art of Assembly ...

    Itanium Architecture For Programmers

    DECNUM: Converting an Integer to Decimal Format Section 6.7. Using C for ASCII Input and Output Section 6.8. BACKWARD: Using Byte Manipulations Summary References Exercises Chapter 7. ...

Global site tag (gtag.js) - Google Analytics