Searching through text field

  • I have a text field (nvarchar max) that contains a chat transcript. I need to find the first chat entry that was not entered by one of our agents. That is, the first entry by the customer.

    The transcript will look like this:

    20:32:12 [Rachel] Thank you for contacting Our Company. How may I assist you?

    20:32:31 [Marina Galofaro] What is the mailing address to mail a check?

    20:32:49 [Rachel] I'm happy to provide you with the mailing address.

    20:33:22 [Marina Galofaro] O kay, Thank you.

    20:33:28 [Rachel] One moment please while I provide a link with directions to mail your payment.

    20:34:05 [Rachel] Please click <a href="http://company.custhelp.com/app/answers/detail/a_id/1050/kw/payment" target="_blank">here</a> to view how to mail a payment.

    20:35:07 [Rachel] Is there anything else I can help with, Marina?

    20:35:56 [Marina Galofaro] No, Thank you!

    20:36:00 [Marina Galofaro] Session Disconnected

    So the first thing I need to do is find the name between the first set of brackets and compare the contents to another table that translates an ID on this table to a name.

    If the name is found, then I need to move on to the next bracketed section and find that name and compare it. If it matches again, then move on to the next bracket, and so on. Once the name does not match, then I need to extract that first line of the chat, including the HTML tags. So basically back up 13 chars from that bracket and then grab the text through the next

    tag.

    I have something worked out for the first part to get that first bracketed name and compare it to the agent on the record.

    SELECT A.ID

    , A.Agent

    , A.TextHTML

    , charindex('[', TextHTML, 0) as FirstOpenBracket

    , charindex(']', TextHTML, 0) as FirstClosedBracket

    , SUBSTRING(TextHTML, CHARINDEX('[', TextHTML , 1) + 1, CASE WHEN (CHARINDEX(']', TextHTML , 0) - CHARINDEX('[', TextHTML, 0)) > 0 THEN CHARINDEX(']', TextHTML, 0) - CHARINDEX('[', TextHTML, 0) - 1 ELSE 0 END) as FirstBracketName

    , U.FirstName as AgentFirstName

    FROM Actions A INNER JOIN Users U on A.Agent = U.ID

    WHERE A.[Type] = 9 AND A.CreationTime >= '2012-10-26 00:00:00' AND A.TimeAllocated > 0 AND A.TextHTML IS NOT NULL

    But I don’t know where to go from there.

    Can this even be accomplished “in line” ?

    I don’t think so since the number of bracketed names until I hit the customer is dynamic.

    Could anyone point me in the right direction?

    Thanks

    Paul

  • Perhaps this will help you find the path?

    ;WITH Transcript (message) AS (

    SELECT '20:32:12 [Rachel] Thank you for contacting Our Company. How may I assist you?'

    UNION ALL SELECT '20:32:31 [Marina Galofaro] What is the mailing address to mail a check?'

    UNION ALL SELECT '20:32:49 [Rachel] I''m happy to provide you with the mailing address.'

    UNION ALL SELECT '20:33:22 [Marina Galofaro] O kay, Thank you.'

    UNION ALL SELECT '20:33:28 [Rachel] One moment please while I provide a link with directions to mail your payment.'

    UNION ALL SELECT '20:34:05 [Rachel] Please click <a href="http://company.custhelp.com/app/answers/detail/a_id/1050/kw/payment" target="_blank">here</a> to view how to mail a payment.'

    UNION ALL SELECT '20:35:07 [Rachel] Is there anything else I can help with, Marina? '

    UNION ALL SELECT '20:35:56 [Marina Galofaro] No, Thank you!'

    UNION ALL SELECT '20:36:00 [Marina Galofaro] Session Disconnected'),

    Operators (username) AS (

    SELECT 'Rachel')

    SELECT message, username

    ,operator=LEFT(operator, CHARINDEX(']', operator)-1)

    ,CHARINDEX(']', operator)

    ,TextOfMessage=LTRIM(RIGHT(operator, LEN(operator)-CHARINDEX(']', operator)))

    FROM Transcript

    CROSS APPLY (

    SELECT SUBSTRING(message, CHARINDEX('[', message)+1, LEN(message))

    ) a(operator)

    LEFT JOIN Operators ON LEFT(operator, CHARINDEX(']', operator)-1)=username

    WHERE username IS NULL


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • What happens when the customer is called Rachel?

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Great question. In most cases, the customer will be first and last name. But since this is not guaranteed, yes, there will be situations where the wrong line is picked up. We will just have to explain that this is not perfect. There really are no other options at this point.

  • Ok, I admit, I have no idea what this is doing.

    I tweaked the code to what i thought would work (note that i added the WHERE ID = 6831908 just to limit to one row for testing)

    But I get nothing back. Also, i do need to include the HTML tags before and after the first chat line from the customer.

    WITH Transcript (message) AS (

    SELECT TextHTML FROM Actions WHERE ID = 6831908),

    Operators (FirstName) AS (

    SELECT FirstName FROM Actions A INNER JOIN Users U on A.Agent = U.ID WHERE A.ID = 6831908)

    SELECT message, FirstName

    ,operator=LEFT(operator, CHARINDEX(']', operator)-1)

    ,CHARINDEX(']', operator)

    ,TextOfMessage=LTRIM(RIGHT(operator, LEN(operator)-CHARINDEX(']', operator)))

    FROM Transcript

    CROSS APPLY (

    SELECT SUBSTRING(message, CHARINDEX('[', message)+1, LEN(message))

    ) a(operator)

    LEFT JOIN Operators ON LEFT(operator, CHARINDEX(']', operator)-1)=FirstName

    WHERE FirstName IS NULL

  • This is mostly due to us being forced to speculate on your table. Dwain assume you have multiple rows but my guess is your table holds the entire discussion more in a format like this?

    ;WITH Transcript (message) AS (

    SELECT '20:32:12 [Rachel] Thank you for contacting Our Company. How may I assist you?'

    + char(10) + char(13) + '20:32:31 [Marina Galofaro] What is the mailing address to mail a check?'

    + char(10) + char(13) + '20:32:49 [Rachel] I''m happy to provide you with the mailing address.'

    + char(10) + char(13) + '20:33:22 [Marina Galofaro] O kay, Thank you.'

    + char(10) + char(13) + '20:33:28 [Rachel] One moment please while I provide a link with directions to mail your payment.'

    + char(10) + char(13) + '20:34:05 [Rachel] Please click <a href="http://company.custhelp.com/app/answers/detail/a_id/1050/kw/payment" target="_blank">here</a> to view how to mail a payment.'

    + char(10) + char(13) + '20:35:07 [Rachel] Is there anything else I can help with, Marina? '

    + char(10) + char(13) + '20:35:56 [Marina Galofaro] No, Thank you!'

    + char(10) + char(13) + '20:36:00 [Marina Galofaro] Session Disconnected')

    select * from transcript

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Oh hell, i see the problem. I posted the raw data in my first post, but it acted on the HTML tags. I did not even notice that.

    I cant seen to get it to post with the HTML tags.

  • Then you can combine that with Dwain's logic and voila!!!

    ;WITH Actions (message) AS (

    SELECT '20:32:12 [Rachel] Thank you for contacting Our Company. How may I assist you?'

    + char(10) + char(13) + '20:32:31 [Marina Galofaro] What is the mailing address to mail a check?'

    + char(10) + char(13) + '20:32:49 [Rachel] I''m happy to provide you with the mailing address.'

    + char(10) + char(13) + '20:33:22 [Marina Galofaro] O kay, Thank you.'

    + char(10) + char(13) + '20:33:28 [Rachel] One moment please while I provide a link with directions to mail your payment.'

    + char(10) + char(13) + '20:34:05 [Rachel] Please click <a href="http://company.custhelp.com/app/answers/detail/a_id/1050/kw/payment" target="_blank">here</a> to view how to mail a payment.'

    + char(10) + char(13) + '20:35:07 [Rachel] Is there anything else I can help with, Marina? '

    + char(10) + char(13) + '20:35:56 [Marina Galofaro] No, Thank you!'

    + char(10) + char(13) + '20:36:00 [Marina Galofaro] Session Disconnected')

    , Transcript(Message) as

    (

    select Item

    from Actions

    cross apply dbo.DelimitedSplit8K(replace(message, char(13), ''), char(10))

    )

    ,

    Operators (username) AS

    (

    SELECT 'Rachel'

    )

    SELECT message, username

    ,operator=LEFT(operator, CHARINDEX(']', operator)-1)

    ,CHARINDEX(']', operator)

    ,TextOfMessage=LTRIM(RIGHT(operator, LEN(operator)-CHARINDEX(']', operator)))

    FROM Transcript

    CROSS APPLY (

    SELECT SUBSTRING(message, CHARINDEX('[', message)+1, LEN(message))

    ) a(operator)

    LEFT JOIN Operators ON LEFT(operator, CHARINDEX(']', operator)-1)=username

    WHERE username IS NULL

    You can find the DelimitedSplit8K by following the article in my signature about splitting strings.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Oh my gosh how do i post this text field contents without it running the HTML tags???

  • paul.j.kemna (1/31/2013)


    Oh my gosh how do i post this text field contents without it running the HTML tags???

    I think if you put it inside the code tag it should work ok.

    <-- Use if IFCode shortcuts over there on the left when posting.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I have tried a bunch of those tags and they all still process the HTML tags in my example.

  • paul.j.kemna (1/31/2013)


    I have tried a bunch of those tags and they all still process the HTML tags in my example.

    So put in a space or an underscore or something so you can post it. Just let us know what we need to do to fix it.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • OK, i put an underscore inside every HTML tag. This is what the field looks like. I need to find the first customer entry, including the HTML tags at the beginning and end. In the case below, it would be

    <_BR>20:32:31 [Marina Galofaro] What is the mailing address to mail a check? <_BR>

    I will determine whether it is the customer or not by comparing the value between the [] to a value on another table via join on an ID on this table to an ID on that table. (see original post).

    SELECT '<_BR>20:32:12 [Rachel] Thank you for contacting Scholastic. How may I assist you?<_BR><_BR>20:32:31 [Marina Galofaro] What is the mailing address to mail a check? <_BR><_BR>20:32:49 [Rachel] Im happy to provide you with the mailing address.<_BR><_BR>20:33:22 [Marina Galofaro] O kay, Thank you. <_BR><_BR>20:33:28 [Rachel] One moment please while I provide a link with directions to mail your payment.<_BR><_BR>20:34:05 [Rachel] Please click <_a href="http://scholastic.custhelp.com/app/answers/detail/a_id/1050/kw/payment" target="_blank">here<_/a> to view how to mail a payment.<_BR><_BR>20:35:07 [Rachel] Is there anything else I can help with, Marina? <_BR><_BR>20:35:56 [Marina Galofaro] No, Thank you!<_BR><_BR>20:36:00 [Marina Galofaro] Session Disconnected<_BR><_BR>' as TextHTML

  • I don't get what "operator" is in these examples. I would also prefer to not use the splitter function.

    I am to the point where i may have to rely on a a cursor and just parse the text line by line.

  • paul.j.kemna (2/1/2013)


    I don't get what "operator" is in these examples. I would also prefer to not use the splitter function.

    I am to the point where i may have to rely on a a cursor and just parse the text line by line.

    paul.j.kemna (2/1/2013)


    I don't get what "operator" is in these examples. I would also prefer to not use the splitter function.

    I am to the point where i may have to rely on a a cursor and just parse the text line by line.

    Well here is the rub. Not only is a cursor going to perform far worse, you have only 1 row of data so you can't use a cursor anyway. You need to split your text lines into separate rows first.

    This seems to work on your example.

    ;WITH Actions (message) AS

    (

    ;WITH Actions (message) AS

    (

    SELECT '< br>20:32:12 [Rachel] Thank you for contacting Our Company. How may I assist you?< br>< br>20:32:31 [Marina Galofaro] What is the mailing address to mail a check?< br>< br>20:32:49 [Rachel] I''m happy to provide you with the mailing address.< br>< br>20:33:22 [Marina Galofaro] O kay, Thank you.< br>< br>20:33:28 [Rachel] One moment please while I provide a link with directions to mail your payment.< br>< br>20:34:05 [Rachel] Please click <a href="http://company.custhelp.com/app/answers/detail/a_id/1050/kw/payment" target="_blank">here</a> to view how to mail a payment.< br>< br>20:35:07 [Rachel] Is there anything else I can help with, Marina? < br>< br>20:35:56 [Marina Galofaro] No, Thank you!< br>< br>20:36:00 [Marina Galofaro] Session Disconnected< br>'

    )

    ,Transcript(Message) as

    (

    select Item

    from Actions

    cross apply dbo.DelimitedSplit8K(replace(replace(message, '< br>< br>', char(10)), '< br>', ''), char(10))

    )

    ,Operators (username) AS

    (

    SELECT 'Rachel'

    )

    SELECT

    operator = LEFT(operator, CHARINDEX(']', operator)-1)

    ,TextOfMessage = LTRIM(RIGHT(operator, LEN(operator)-CHARINDEX(']', operator)))

    FROM Transcript

    CROSS APPLY

    (

    SELECT SUBSTRING(message, CHARINDEX('[', message)+1, LEN(message)) as operator

    ) a

    LEFT JOIN Operators ON LEFT(operator, CHARINDEX(']', operator)-1) = username

    WHERE username IS NULL

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 15 posts - 1 through 15 (of 21 total)

You must be logged in to reply to this topic. Login to reply