Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Extracting Data from varchard field

Posted on 2011-09-22
16
Medium Priority
?
298 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
ID: 36579341
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
ID: 36579510
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
ID: 36579522
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:smu95rp
ID: 36579548
I meant to say MISunderstood.
0
 

Author Comment

by:halifaxman
ID: 36579550
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
ID: 36579554
Yes it's the same.
0
 
LVL 2

Expert Comment

by:smu95rp
ID: 36579575
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
ID: 36579581
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
 
LVL 2

Expert Comment

by:smu95rp
ID: 36579603
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
ID: 36579623
D'oh or even simpler:

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

Author Comment

by:halifaxman
ID: 36579665
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 2000 total points
ID: 36579843
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
ID: 36579880
Brilliant Thanks Worked a treat, thanks for your help on this much appreciated
0
 
LVL 2

Expert Comment

by:smu95rp
ID: 36579884
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
ID: 36579885
You're welcome.
0
 

Author Closing Comment

by:halifaxman
ID: 36579887
Thanks for the effort on this one
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

916 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