Solved

ORACLE SQL Zip-code split

Posted on 2009-05-08
4
1,525 Views
Last Modified: 2013-12-07
I have the following (hopefully simple) SQL question.  I have a field which holds the zip code in ZIP5-ZIP4 format.  I would like to Select this field into TWO fields (one for ZIP5 and one for ZIP4).  The two ZIP fields have a "-" between them.  This is selecting from an Oracle database.How do I do this?
Thanks
0
Comment
Question by:mbroad02
4 Comments
 
LVL 11

Accepted Solution

by:
Andytw earned 500 total points
ID: 24337201
The Oracle syntax would be:
select substr(zips, 0, instr(zips,'-') - 1) zip5,
           substr(zips, instr(zips,'-') + 1) zip4
from t
 SQL> create table t(zips varchar2(512));
Table created.
SQL> insert into t values ('3434-21444');
1 row created.
SQL> insert into t values ('956-1112');
1 row created.
SQL> select substr(zips, 0, instr(zips,'-') - 1) zip5,
  2         substr(zips, instr(zips,'-') + 1) zip4
  3  from t;
 
ZIP5            ZIP4
--------------- ---------------
3434            21444
956             1112
SQL>

Open in new window

0
 

Author Closing Comment

by:mbroad02
ID: 31579498
Perfect!!! Thank you!!!
0
 

Expert Comment

by:greyhuman
ID: 24337257
If you always know that there will be values in the column then use this simple
SUBSTRING function.
select substr('12345-1234',1,5), substr('12345-1234',7)from dual;

If you think that there may be values missing as ZIP4 may be ,then use INSTR or SUBSTR
0
 
LVL 16

Expert Comment

by:Milleniumaire
ID: 24337263
Try this:

update mytable
set zip4 = substr(zip,1,instr(zip,'-')-1),
      zip5 = substr(zip,instr(zip,'-')+1)
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Character matching different date formats for dates between 6 59
Oracle Insert not working 10 32
format dd/mm/yyyy parameter 16 31
Oracle Nested table uses ? 2 34
I showed you how to use console view (HERE (http://www.experts-exchange.com/articles/18379/Getting-Started-and-Using-the-Salesforce-com-Console.html)) -– but how do you set it up on the admin side of Salesforce? Note that you have to have Admin leve…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question