Solved

String Manipulation

Posted on 2011-02-17
4
366 Views
Last Modified: 2012-05-11
I'm trying to manipulate a string column with string data like this:

sometimes the first number before the ';' is a single digit, but sometimes it can be above 10

1;49yihjkflo5968gjvn,product1,01/01/2011 -- Single Digit before the ';'
10;4568ujghty574ifk42,product4,01/09/2011 -- Double Digit before the ';'

what i'm trying to do is first select the account number, alwas 18 characters, which stars just after the first ';' then, for the second column the everthing before the first ';' coulbe be 1 or 2 digits.

so outcome of my query should look something like this:

ACCOUNT_ID                     PRODUCT_INFO
49yihjkflo5968gjvn             1;product1,01/01/2011
4568ujghty574ifk42            10;product4,01/09/2011

i've tried it like this, but it chops off either the 2nd digit of the Number of product_info, or it chops off the last digit of the account_id if the first digit is two characters.

select substring(MM_Prod_Info,3,18)as AccountID, substring(MM_Prod_Info, charindex(';',MM_Prod_Info)-1,len(MM_Prod_Info)) as ProductInfo from mm_prod_codes

select substring(MM_Prod_Info,3,18)as AccountID, substring(MM_Prod_Info, charindex(';',MM_Prod_Info)-1,len(MM_Prod_Info)) as ProductInfo from mm_prod_codes

Open in new window

0
Comment
Question by:basile
  • 2
4 Comments
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 34922798
SELECT
	SUBSTRING(MM_Prod_Info, CHARINDEX(';', MM_Prod_Info) +1 , 18) AS ACCOUNT_ID,
	REPLACE(MM_Prod_Info, SUBSTRING(MM_Prod_Info, CHARINDEX(';', MM_Prod_Info) , 19), '') AS PRODUCT_INFO
FROM
	mm_prod_codes

Open in new window

0
 
LVL 12

Expert Comment

by:mcv22
ID: 34922815

SELECT
	SUBSTRING(MM_Prod_Info, CHARINDEX(';', MM_Prod_Info) + 1, 18) AS Account_ID,
	SUBSTRING(MM_Prod_Info, 1, CHARINDEX(';', MM_Prod_Info)) + SUBSTRING(MM_Prod_Info, CHARINDEX(',', MM_Prod_Info) + 1, LEN(MM_Prod_Info)) as ProductInfo 
FROM
	mm_prod_codes

Open in new window

0
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 500 total points
ID: 34922829
a little mistake, here is the fix

SELECT
	SUBSTRING(MM_Prod_Info, CHARINDEX(';', MM_Prod_Info) +1 , 18) AS ACCOUNT_ID,
	REPLACE(MM_Prod_Info, SUBSTRING(MM_Prod_Info, CHARINDEX(';', MM_Prod_Info) +1 , 19), '') AS PRODUCT_INFO
FROM
	mm_prod_codes

Open in new window

0
 
LVL 1

Author Closing Comment

by:basile
ID: 34923059
Thanks that was great and useful!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need Counts 11 40
SQL 2012 Syntax Error 5 24
MS SQL 2014 get SPIDs of users 6 26
Sql query for filter 12 22
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

706 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

18 Experts available now in Live!

Get 1:1 Help Now