Solved

ORACLE SQL Zip-code split

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
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.

862 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

27 Experts available now in Live!

Get 1:1 Help Now