Solved

Split data from one field into two fields

Posted on 2009-07-02
7
456 Views
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 -
218469-001
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?
0
Comment
Question by:UOC
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:kevin_u
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.
0
 

Author Comment

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

I am using SQL7
0
 
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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Accepted Solution

by:
kevin_u earned 500 total points
ID: 24769266
sorry...  substring.
0
 
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

0
 
LVL 59

Expert Comment

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

Author Closing Comment

by:UOC
ID: 31599434
Well done. Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 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