Solved

String Manipulation

Posted on 2011-02-17
4
370 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:Auerelio Vasquez
[X]
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
  • 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:Auerelio Vasquez
ID: 34923059
Thanks that was great and useful!
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

710 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