Solved

ORACLE SQL Zip-code split

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

Suggested Solutions

Title # Comments Views Activity
Oracle and DateTime math 6 39
Oracle Public Synonyms and Privileges 2 66
database upgrade 8 77
Require data to appear on a single line 2 44
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

803 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