String Manipulation - Data Between Quotes

  • Hi, I have data in a trace file, and I need to extract some info such as phone number.

    The problem is the phone number could be varying lengths, and various positions in the row.

    For example:

    @City='New York', @Phone='2035551212' (10 characters, no dashes)

    or

    @City='San Francisco', @Phone='918-555-1212' (12 characters, with dashes)

    or

    @City+'Berlin', @Phone='55-123456-7890' (14 characters, with dashes)

    I can use CHARINDEX to search & find @Phone=' so I know where the phone number starts, but stuck on a programatic way to find the data between the quotes since it can vary.

    Thoughts ??

  • homebrew01 (4/9/2015)


    Hi, I have data in a trace file, and I need to extract some info such as phone number.

    The problem is the phone number could be varying lengths, and various positions in the row.

    For example:

    @City='New York', @Phone='2035551212' (10 characters, no dashes)

    or

    @City='San Francisco', @Phone='918-555-1212' (12 characters, with dashes)

    I can use CHARINDEX to search & find @Phone=' so I know where the phone number starts, but stuck on a programatic way to find the data between the quotes since it can vary.

    Thoughts ??

    Can you post a representative string you are trying to search? CHARINDEX and SUBSTRING is the answer but I can't tell what your string look like.

    _______________________________________________________________

    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/

  • Something like this should help.

    SELECT String,

    SUBSTRING( String, Start.Position, CHARINDEX( '''', String, Start.Position + 1) - Start.Position)

    FROM (VALUES( '@City=''New York'', @Phone=''2035551212'' (10 characters, no dashes)'),

    ('@City=''San Francisco'', @Phone=''918-555-1212'' (12 characters, with dashes)'))x(String)

    CROSS APPLY (SELECT CHARINDEX( '@Phone=''', String) + 8)start(Position)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Or maybe like this:

    SELECT String,

    LEFT( Phone.number, CHARINDEX( '''', Phone.number) - 1)

    FROM (VALUES( '@City=''New York'', @Phone=''2035551212'' (10 characters, no dashes)'),

    ('@City=''San Francisco'', @Phone=''918-555-1212'' (12 characters, with dashes)'))x(String)

    CROSS APPLY (SELECT STUFF( String, 1, CHARINDEX( '@Phone=''', String) + 7, ''))Phone(number)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Some simple solutions have already been posted, but there are also some queries/string manipulation techniques in this article that would help you pull any of the data out of your strings:

    Variable Default Values to a Stored Procedure [/url]

    In the end you could use a REPLACE to get rid of the quotation characters.


    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

Viewing 5 posts - 1 through 4 (of 4 total)

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