Splitting a field in the middle ??

  • Hi

    I have a field which looks like "LastName, FirstName (DOB: 01/01/1900)"

    How do I get the "01/01/1900" between ":" and ")"

    Thanks in Advance

    Joe

  • jbalbo (3/19/2015)


    Hi

    I have a field which looks like "LastName, FirstName (DOB: 01/01/1900)"

    How do I get the "01/01/1900" between ":" and ")"

    Thanks in Advance

    Joe

    I'd try something like:

    DECLARE @TestString VARCHAR(50)

    SET @TestString = 'LastName, FirstName (DOB: 01/01/1900)'

    SELECT SUBSTRING(@TestString, CHARINDEX(':', @TestString) + 2, CHARINDEX(')', @TestString) - CHARINDEX(':', @TestString) - 2)



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • That works great, Thank You....

    And if I looked at the table "Just a bit" closer I would have seen that DOB was there... lol

    But I did learn something new!

    Thanks Again

  • These problems are fun. If the data format is always the same (##/##/####) you could also do this:

    DECLARE @TestString VARCHAR(50)

    SET @TestString = 'LastName, FirstName (DOB: 01/01/1900)'

    SELECT SUBSTRING(@TestString, PATINDEX('%[0-1][1-9]/%', @TestString),10);

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • Alan.B (3/19/2015)


    These problems are fun. If the data format is always the same (##/##/####) you could also do this:

    DECLARE @TestString VARCHAR(50)

    SET @TestString = 'LastName, FirstName (DOB: 01/01/1900)'

    SELECT SUBSTRING(@TestString, PATINDEX('%[0-1][1-9]/%', @TestString),10);

    I agree. It can be fun to see what other ways people come up with as an answer to string manipulation in sql.

    Here is one:

    select left(ltrim(PARSENAME(replace(@TestString, ':', '.'), 1)), 10)

    --edit--

    typo...again

    _______________________________________________________________

    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 will definitely be able to use this for something, but I did find a problem with the formatting,

    I got en error because one record had parenthesis in the name i.e.. (John Smith(aka jack) DOB: 01/01/1900)

    Any ideas if not in the same structure?

  • jbalbo (3/19/2015)


    I will definitely be able to use this for something, but I did find a problem with the formatting,

    I got en error because one record had parenthesis in the name i.e.. (John Smith(aka jack) DOB: 01/01/1900)

    Any ideas if not in the same structure?

    AS long as there's no ":" in the string, this revised code should work:

    DECLARE @TestString VARCHAR(50)

    SET @TestString = 'LastName(s), FirstName(s) (DOB: 01/01/1900)'

    SELECT SUBSTRING(@TestString, CHARINDEX(':', @TestString) + 2, CHARINDEX(')', @TestString, CHARINDEX(':', @TestString)) - CHARINDEX(':', @TestString) - 2)

    This should work even if there is one or more ":" in the names:

    DECLARE @TestString VARCHAR(50)

    SET @TestString = 'LastName(s), FirstName(s) (DOB: 01/01/1900)'

    SELECT SUBSTRING(@TestString, CHARINDEX('DOB:', @TestString) + 5, CHARINDEX(')', @TestString, CHARINDEX('DOB:', @TestString)) - CHARINDEX('DOB:', @TestString) - 5)



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 7 posts - 1 through 6 (of 6 total)

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