Split data from one field into two fields

Posted on 2009-07-02
Last Modified: 2012-05-07
I have a field 'called patno_samno' in an SQL 7 table called 'anal_06' which has data in the following format -
I want to put the first part (218469) into another field called 'patno1' and the second part (001) into a field called 'samno1'. There are thousands of these entries. The first part (the patno) all have 6 characters and the second (the samno) all have 3 characters and both are seperated by a dash in the 'patno_samno' field.

How do I copy the required data to seperate fields please?
Question by:UOC
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 12

Expert Comment

ID: 24769152
create the new fields in the database

update anal_06 set patno1 = substr(patno_samno,1,6), samno1 = substr(patno_samno, 8, 3)

you can add a where clause to do it on subsets of data.

Author Comment

ID: 24769204
'substr' is not a recognized function name.

I am using SQL7
LVL 59

Expert Comment

by:Kevin Cross
ID: 24769243
Try with substring(str, i, n) where i is starting index and n is count of chars.
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

LVL 12

Accepted Solution

kevin_u earned 500 total points
ID: 24769266
sorry...  substring.
LVL 59

Expert Comment

by:Kevin Cross
ID: 24769274
If this is variable length and you need to split on hyphen ('-') then just do like this:
update anal_06
set patno1 = left(patno_samno, charindex('-', patno_samno)-1)
, samno1 = right(patno_samno, charindex('-', reverse(patno_samno))-1)
-- or with fixed widths, simply
update anal_06
set patno1 = left(patno_samno, 6)
, samno1 = right(patno_samno, 3);

Open in new window

LVL 59

Expert Comment

by:Kevin Cross
ID: 24769276
@kevin_u, welcome to EE!

Author Closing Comment

ID: 31599434
Well done. Thanks!

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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