Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

Extracting Data from varchard field


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
halifaxman
Asked:
halifaxman
  • 10
  • 6
1 Solution
 
smu95rpCommented:
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
 
halifaxmanAuthor Commented:
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
 
smu95rpCommented:
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
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
smu95rpCommented:
I meant to say MISunderstood.
0
 
halifaxmanAuthor Commented:
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
 
smu95rpCommented:
Yes it's the same.
0
 
smu95rpCommented:
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
 
halifaxmanAuthor Commented:
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
 
smu95rpCommented:
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
 
smu95rpCommented:
D'oh or even simpler:

ltrim(right(string, patindex('%:A%',reverse(string)) -2 ))
0
 
halifaxmanAuthor Commented:
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
 
smu95rpCommented:
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
 
halifaxmanAuthor Commented:
Brilliant Thanks Worked a treat, thanks for your help on this much appreciated
0
 
smu95rpCommented:
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
 
smu95rpCommented:
You're welcome.
0
 
halifaxmanAuthor Commented:
Thanks for the effort on this one
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now