`

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;

分享到:
评论

相关推荐

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

    - 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 ...

    Your Excel Survival Kit: Your Guide to Surviving and Thriving in an Excel world

    Converting Unpivoted Data to a Pivoted Format Creating a Query in Power Query to Merge Data Sets Summary Chapter 7 Beyond the Pivot Table:Power Pivot Instaling and Locating Power Pivot Before You ...

    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(). ...

    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 ...

    Python Cookbook英文版

    Numbers 8.9 Using dtuple for Flexible Access to Query Results 8.10 Pretty-Printing the Contents of Database Cursors 8.11 Establishing Database Connections Lazily 8.12 Accessing a JDBC Database...

    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 ...

    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 ...

    IOS5 Programming Cookbook

    6.7 Converting Longitude and Latitude to a Meaningful Address 452 6.8 Converting Meaningful Addresses to Longitude and Latitude 455 7. Implementing Gesture Recognizers . . . . . . . . . . . . . . . . ...

    Big Data Analytics with Applications in Insider Threat Detection-CRC(2018).pdf

    For this series, we are converting some of the practical aspects of our work with students into books. The relationships between our texts will be illus- trated in Appendix A. ORGANIZATION OF THIS ...

    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. ...

    php.ini-development

    The number of significant digits displayed in floating point numbers. ; http://php.net/precision precision = 14 ; Output buffering is a mechanism for controlling how much output data ; (excluding ...

    ZendFramework中文文档

    1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) 2.1.4. 注册角色(Role) 2.1.5. 定义访问...

    Advanced Bash-Scripting Guide <>

    Converting DOS Batch Files to Shell Scripts M. Exercises M.1. Analyzing Scripts M.2. Writing Scripts N. Revision History O. Mirror Sites P. To Do List Q. Copyright 表格清单: 11-1. 作业标识符 30-1. ...

Global site tag (gtag.js) - Google Analytics