Advertisement

07.26.2007 at 06:44AM PDT, ID: 22722692
[x]
Attachment Details

Using Patindex substing in t-sql

Asked by Putoch in SQL Server 2005, SQL Query Syntax

Tags: patindex, substring, sql

Hi,
I am hoping someone can help me here.
I am trying to take certin text out of one  text field in a table. This one table holds account history.
By using Substring and Patindex i have been able to see what a customer has moved from and to and put these into two fileds in the select statement. however when i try and do the same to get the price of the product before and after i can not get it to work. I am getting errors:Invalid length parameter passed to the SUBSTRING function.. or no data back altogether.

I have created an example of what i am doing and any sugesstions would be greatly appriciated.
--Create and Insert:
CREATE TABLE  Account_history(history_text text not null);

insert into Account_history (history_text) values  ('<b>Product</b> changed from <b>BOOK 1</b> to <b>BOOK 2</b><BR><b>Recurring Fee</b> changed from <b>6324.00</b> to <b>3255</b>')
--Statement:
select
SUBSTRING(history_text, 32, PATINDEX('%</b>%', SUBSTRING(history_text,32,50))+0) AS MoveFrom_Product,--substring passes all the text up to char 32,PATINDEX passes all the wildcards used
      SUBSTRING(history_text,32+PATINDEX('%<b>%',SUBSTRING(history_text,32,50))+2,
 PATINDEX('%</b>%',SUBSTRING(history_text,32+PATINDEX('%<b>%',SUBSTRING(history_text,32,50))+2,500))-1)AS MovedTo_product
 FROM Account_history
WHERE history_text LIKE '<b>Product</b> changed from%to%'
      AND history_text NOT LIKE '<b>Product</b> changed from <b></b> to <b>%'

Start Free Trial
 
 
Loading Advertisement...
 
[+][-]07.26.2007 at 07:33AM PDT, ID: 19574803

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 07:53AM PDT, ID: 19575006

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 08:13AM PDT, ID: 19575209

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 09:17AM PDT, ID: 19575880

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.27.2007 at 12:30AM PDT, ID: 19580698

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, SQL Query Syntax
Tags: patindex, substring, sql
Sign Up Now!
Solution Provided By: patrikt
Participating Experts: 1
Solution Grade: B
 
 
[+][-]07.30.2007 at 03:24AM PDT, ID: 19591324

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32