Solved

ORACLE SQL Zip-code split

Posted on 2009-05-08
4
1,499 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
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

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now