Home > Ask the Oracle Experts > SQL Questions & Answers > SQL string functions
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

SQL string functions

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 03 June 2008
I have a field which is char(12) and numeric value stored. The value could be numeric 9 or numeric 12. I am interested in the last two digits of numeric field. How can I use select statement to pick up those records which the last two digit are "21"?

>
EXPERT RESPONSE
To set the stage for the answer, let's look at a couple of exampes of the data in your column.

Because the column has the CHAR datatype, all values in the column are "padded" with enough trailing spaces to fill out the entire 12 characters. Let's use the special character "§" to represent a space. You said the values could be numeric 9 or numeric 12, which means that the values in that column would look like this:

123456789§§§
111122223333
937937937§§§

Notice that the 9-digit numbers occupy the leftmost 9 characters of the column, with trailing spaces.

The solution you're looking for, to extract the last two digits, can be accomplished using the RIGHT string function. This is not a standard SQL function, and not every database system has implemented it, but it works in all the popular ones like Oracle, SQL Server, MySQL, et cetera.

But before we can use RIGHT, we have to do something about those trailing spaces. Standard SQL has the TRIM function, which is exactly what we need:

TRIM(TRAILING ' ' FROM column)

Again, popular database systems have all implemented a TRIM function, all of which default to trimming spaces (if no other character is specified). Some database systems allow you to use a function called RTRIM, which trims from the right.

Putting both functions together, we get:


RIGHT(RTRIM(column),2)

This produces these results on our sample values:

89
33
37

Thus, in order to find values where the last two digits are 21, use:

SELECT ...
  FROM ...
 WHERE RIGHT(RTRIM(column),2) = '21'

SQL string functions can be nested, as shown.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table
SQL to select rows 1000 through 3000 in a table
SQL query to combine rows
The SQL REPLACE function
CASE expressions in the ORDER BY clause
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL

Oracle development languages
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table
SQL to select rows 1000 through 3000 in a table
SQL query to combine rows
The SQL REPLACE function
CASE expressions in the ORDER BY clause
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts