Q
Problem solve Get help with specific problems with your technologies, process and projects.

Numbers in a VARCHAR2 column

How do I query an Oracle table to find which rows contain numeric data in a column of type VARCHAR2?
See the earlier question on finding what rows contain valid dates for an explanation of the "run it up the flagpole and see who salutes" approach used in the function below.

```CREATE OR REPLACE FUNCTION  to_num
(
in_num_text  IN  VARCHAR2,
in_fmt_text  IN  VARCHAR2
DEFAULT NULL
)
RETURN  NUMBER

--  *******************
--  **  t o _ n u m  **
--  *******************

--  to_num returns the value of in_num_text if it is a number in
--    the format specified by in_fmt_text, NULL if it is not.
--  If in_fmt_text is omitted, to_num will return a number if
--    in_num_text is a number in ANY usual format, e.g. '5', '+5',
--    '5.00', '.5e1' all return 5.

IS
BEGIN
IF  in_fmt_text  IS NULL
THEN
RETURN  TO_NUMBER ( in_num_text );
ELSE
RETURN  TO_NUMBER ( in_num_text
, in_fmt_text
);
END IF;
EXCEPTION
WHEN OTHERS
THEN
RETURN  NULL;
END   to_num;
/

SELECT      '=>'          -- to make leading spaces clear
||  text
||  '<='
, to_num (text)
, to_num (text, '999.999')
FROM    number_test
ORDER BY
text;

'=>'||TEXT||'<='         TO_NUM(TEXT) TO_NUM(TEXT,'999.999')
------------------------ ------------ ----------------------
=> 1 <=                             1
=>\$10<=
=>+56<=                            56
=>2.3<=                           2.3                    2.3
=>4,000<=
=>5+6<=
=>7.888888<=                 7.888888
=>9.1e2<=                         910
=>MMIII<=
=><=
```

One important difference between TO_NUMBER and TO_DATE is that TO_DATE is always working with a particular format in mind. If you don't pass a format model to TO_DATE, it will use the system default. In other words, "SELECT TO_DATE (col_a) FROM table_x" will always result in an error if one row has '22-Dec-2003' and another has '2003-12-22' in col_a. TO_NUMBER, as shown above, is happy with such dissimilar formats as ' 1 ' and '9.1e2'.

Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

SearchDataManagement

• MariaDB X4 brings smart transactions to open source database

Open source database vendor MariaDB updates its flagship platform with new features to enable the convergence of transactional ...

• How data lineage tools boost data governance policies

Organizations can bolster data governance efforts by tracking the lineage of data in their systems. Get advice on how to do so ...

• Proactive practices for data quality improvement

Instead of waiting for data quality to become an issue, consider a proactive approach. Here are some ways to improve data quality...

• Storytelling using data makes information easy to digest

In a Q&A, Nate Nichols and Anna Schena Walsh of AI-based analytics vendor Narrative Science talk about how data storytelling can ...

• Data-driven storytelling makes data accessible

In a Q&A, Nate Nichols and Anna Schena Walsh of data storytelling vendor Narrative Science discuss their book on storytelling and...

• Startup Uplevel targets software engineering efficiency

Startup vendor Uplevel emerged from stealth on Wednesday touting a platform aimed at improving the efficiency of software ...

SearchSAP

• SAP S/4HANA migration: Critical advice for moving off ECC

With the end of SAP ECC support looming in 2025, organizations must make some tough decisions. Here's a look at your choices.

• New SAP leadership faces big challenges in 2020

Industry analysts discuss SAP's biggest issues in 2020, including how the two new CEOs will guide the company deeper into the ...

• SAP Data Hub opens predictive possibilities at Paul Hartmann

When medical supply firm Paul Hartmann AG tested a supply chain analysis system built on SAP Data Hub, it found that it could ...

SearchSQLServer

• SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

TheServerSide.com

• Don't ever put a non-Java LTS release into production

Development teams should avoid non-long-term support releases at all costs. Pay attention to the Java release cycle to make sure ...

• Public API strategy considerations for enterprise adoption

As organizations look for more cost-effective ways to manage data, an evolving landscape with APIs has made the technology more ...

• Ideas on how to hold a successful code hackathon

Want to host a hackathon? Here are some ideas on what a company can do to host an event that solves problems and reenergizes the ...

SearchDataCenter

• Organizations try to predict the effect of 5G infrastructure

With more 5G and IoT devices emerging, admins must prepare their data centers to support low-latency apps and edge computing with...

• Top infrastructure and operations technology myths of 2019

Admins are consistently evaluating technology to improve I&O efficiency. Cost, integration and business goals are key components ...

• Improve efficiency with server energy consumption tools

Deciding what servers are most efficient for your infrastructure requires research. Hardware-level certifications and web-based ...

SearchContentManagement

• AI can enhance content security with a bit of planning

Microsoft and Box both use AI technologies to keep content secure in the cloud. But before using such tools, businesses first ...

• Ex-SAP exec steers Episerver CMS toward digital experience market

Alex Atzberger discusses leaving the helm of SAP's CX platform to become Episerver CEO. Now, Episerver looks to reinvent itself ...

• 4 digital transformation terms to know

As technology evolves, businesses have no choice but to follow. Digital transformations are inevitable in order to keep customers...

SearchHRSoftware

• Why mobile recruiting is the future

Recruiters can use text recruiting to connect with great candidates. Here's a look at how mobile recruiting works, why it's ...

• Top 7 HR trends for 2020 and beyond: The change decade

Because HR requires a balance between the needs of the organization with the needs of employees, it's always been a balancing act...

• For insider threat programs, HR should provide checks and balances

Insider threat programs may backfire if employees feel they are intrusive and violate privacy, Forrester Research warns. Making ...

Close