Home > Ask the Oracle Database / Applications 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"?


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


RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

Oracle development languages
How to check SQL query construction with the Mimer Validator
Understanding SQL string functions
The top advice from Oracle experts in 2008
What's the difference between an SQL inner join and equijoin?
Using LEFT OUTER JOIN query to get zero row counts in SQL
How to return multiple values for THEN clause in an SQL CASE expression
Can I concatenate row values in SQL?
Should I try to avoid a LEFT OUTER JOIN in SQL?
Tips for derived tables in SQL and using FULL OUTER JOINs
How to write an SQL query for two foreign keys to the same table

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


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.




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



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

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




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