Solved

Extracting Data from varchard field

Posted on 2011-09-22
16
275 Views
Last Modified: 2012-06-27

I am tring to query a field which is of datatype varchar(6000), database sql 2008 R2

Examples of data in the field are

Summary of answers  (click to view all)    Q: Please select your client  A: Billy Smith    Q: Enter date for Billy Smith  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Customer  
Summary of answers  (click to view all)    Q: Please select your client  A: Mark Brown    Q: Enter date for Matt Brown  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Till safe
Summary of answers  (click to view all)    Q: Please select your client  A: Matt Baker    Q: Enter date for Matt Baker  A: 08/10/2009 00:00:00    Q: Select who will store the Task(s)  A: Unknown      

I trying to extract then answer to Q: Select who will store the Task(s) from the field

So for the above examples this would be
A: Customer
A: Till Sale
A: Unknown  

Any help would be cool
0
Comment
Question by:halifaxman
  • 10
  • 6
16 Comments
 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
Would

Select rtrim(substring(<field>, patindex('%Q: Select who will store the Task(s)  A: %',<field>) +41, 6000))
 
(replace <field> with the name of the field or variable)

do what you want?

Bascically, get whatever comes after the string 'Q: Select who will store the Task(s)  A: ' till the end of the field, trimming whitespace from the right.
0
 

Author Comment

by:halifaxman
Comment Utility
Thanks for the answer

At the moment it brings back the below based on the examples above

Q: Q: Please select your client  A: Billy Smith    Q: Enter date for Billy Smith  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Customer
Q: Please select your client  A: Mark Brown    Q: Enter date for Matt Brown  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Till safe
Q: Please select your client  A: Matt Baker    Q: Enter date for Matt Baker  A: 08/10/2009 00:00:00    Q: Select who will store the Task(s)  A: Unknown  
0
 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
I must have understood the content of your field. The below query returns:
Customer
Till safe
Unknown
Declare @table table (string varchar(6000))
insert @table
select'Summary of answers  (click to view all)    Q: Please select your client  A: Billy Smith    Q: Enter date for Billy Smith  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Customer  '
union
select 'Summary of answers  (click to view all)    Q: Please select your client  A: Mark Brown    Q: Enter date for Matt Brown  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Till safe '
union
select 'Summary of answers  (click to view all)    Q: Please select your client  A: Matt Baker    Q: Enter date for Matt Baker  A: 08/10/2009 00:00:00    Q: Select who will store the Task(s)  A: Unknown      '

Select rtrim(substring(string, patindex('%Q: Select who will store the Task(s)  A: %', string) +41, 6000))
From @table

Open in new window

0
 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
I meant to say MISunderstood.
0
 

Author Comment

by:halifaxman
Comment Utility
Thanks for helping

Is the answer above the same as the answer before as I am still getting the same answer as before

rtrim(substring(string, patindex('%Q: Select who will store the Task(s)  A: %', string) +41, 6000))
rtrim(substring(<field>, patindex('%Q: Select who will store the Task(s)  A: %',<field>) +41, 6000))
0
 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
Yes it's the same.
0
 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
I'm guessing the patindex isn't matching the pattern and therefore returning 0: the start of the field.
Can you confirm what results you get if you use the whole of my last query?
0
 

Author Comment

by:halifaxman
Comment Utility
The query pulls back, example below, this the same for all row, there is a space before the Q in each row

 Q: Please select your client  A: Mark Brown    Q: Enter date for Matt Brown  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Till safe
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
OK if the response you want is always the last answer then use this instead:

Select rtrim(reverse(left(reverse(string), patindex('%:A%',reverse(string)) -2 )))

Basically it reverses the string and grabs everything up to the, now, first ':A' pattern and reverses it again.
0
 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
D'oh or even simpler:

ltrim(right(string, patindex('%:A%',reverse(string)) -2 ))
0
 

Author Comment

by:halifaxman
Comment Utility
Thanks for this.

This works 90% of the time

Apologies this is my fault and has only been added in the last couple of days so didnt see it.

There can aslo be data like

Summary of answers  (click to view all)    Q: Please select your client  A: Matt Baker    Q: Enter date for Matt Baker  A: 08/10/2009 00:00:00    Q: Select who will store the Task(s)  A: Unknown  Q: Select Will Storage payment method  A: Annual Paid

i.e. some field have Q: Select Will Storage payment method  A: Annual Paid added on to the end

Apologies for mssing this
0
 
LVL 2

Accepted Solution

by:
smu95rp earned 500 total points
Comment Utility
Ok try:

rtrim(
 substring(string, charindex('A:', string, charindex('Q: Select who will store the Task', string))+ 3,

     isnull(nullif(charindex('Q:', string, charindex('A:', string, charindex('Q: Select who will store the Task', string))) -3 ,-3), len(string))

        - charindex('A:', string, charindex('Q: Select who will store the Task', string))
        )
)


In fact you can replace 'Q: Select who will store the Task'  to extract any of the values (see below code).
Declare @table table (string varchar(6000))
insert @table
select 'Summary of answers  (click to view all)    Q: Please select your client  A: Billy Smith    Q: Enter date for Billy Smith  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Customer  '
union
select 'Summary of answers  (click to view all)    Q: Please select your client  A: Mark Brown    Q: Enter date for Matt Brown  A: 09/12/2009 00:00:00    Q: Select who will store the Task(s)  A: Till safe '
union
select 'Summary of answers  (click to view all)    Q: Please select your client  A: Matt Baker    Q: Enter date for Matt Baker  A: 08/10/2009 00:00:00    Q: Select who will store the Task(s)  A: Unknown  Q: iugui    '

Select 
rtrim(
 substring(string, charindex('A:', string, charindex('Q: Please select your client', string))+ 3, 

     isnull(nullif(charindex('Q:', string, charindex('A:', string, charindex('Q: Please select your client', string))) -3 ,-3), len(string)) 

        - charindex('A:', string, charindex('Q: Please select your client', string))
        )
) [Client],

rtrim(
 substring(string, charindex('A:', string, charindex('Q: Enter date for ', string))+ 3, 

     isnull(nullif(charindex('Q:', string, charindex('A:', string, charindex('Q: Enter date for ', string))) -3 ,-3), len(string)) 

        - charindex('A:', string, charindex('Q: Enter date for ', string))
        )
) [Date],

rtrim(
 substring(string, charindex('A:', string, charindex('Q: Select who will store the Task', string))+ 3, 

     isnull(nullif(charindex('Q:', string, charindex('A:', string, charindex('Q: Select who will store the Task', string))) -3 ,-3), len(string)) 

        - charindex('A:', string, charindex('Q: Select who will store the Task', string))
        )
) [TaskStoreBy]

From @table

Open in new window

0
 

Author Comment

by:halifaxman
Comment Utility
Brilliant Thanks Worked a treat, thanks for your help on this much appreciated
0
 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
You can even wrap each one in a Case statement to ensure you get NULL for any missing values:


case when charindex('Q: Select Will Storage payment method', string) = 0 then null else
rtrim(
 substring(string, charindex('A:', string, charindex('Q: Select Will Storage payment method', string))+ 3,

     isnull(nullif(charindex('Q:', string, charindex('A:', string, charindex('Q: Select Will Storage payment method', string))) -3 ,-3), len(string))

        - charindex('A:', string, charindex('Q: Select Will Storage payment method', string))
        )
) end [PaymentMethod]
0
 
LVL 2

Expert Comment

by:smu95rp
Comment Utility
You're welcome.
0
 

Author Closing Comment

by:halifaxman
Comment Utility
Thanks for the effort on this one
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

772 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

10 Experts available now in Live!

Get 1:1 Help Now