Solved

String Manipulation

Posted on 2011-02-17
4
367 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to save view in SSMS 21 59
sql Total query 2 16
Sql Join Problem 2 32
T-SQL - How to use like '%%' operator to search an integer 2 8
In this article I will describe the Detach & Attach 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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

920 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

11 Experts available now in Live!

Get 1:1 Help Now