T-sql select - like

  • Hello,

    I'm new to these forums & need some help putting together a T-SQL query for selecting some info from my db. On the table I'm attempting to query there is a column called lastname that holds multiple last names for customers. Rows that have more than one last name are separated by a '/' character, no spaces are between the '/' and the last names.

    I want to select customers with two last names, for instance customers like 'garcia/fish' or 'johnson-howard/bobby-ray'.

    Is there a way to select only rows that have one '/' entered in the character string? I don't want last names like 'davis/cameron/lew'. Oh, forgot to mention that the column is of type nvarchar. I assume that something like:

    select lastname

    from HR.Employees

    where lastname LIKE '[/]';

    would select entries that have '/'s however I'm not getting any results & I need ONLY rows that have one '/'

    Hope this makes sense, I realize it's a pretty vague example however I'd be extremely grateful if anyone here can help me.

  • Chain your charindexes.

    Basically, you'll want a where clause that looks like this:

    WHERE

    CHARINDEX( LastName, '/') > 0 -- / exists

    AND CHARINDEX( LastName, '/', CHARINDEX( LastName, '/') + 1) = 0 -- No second /s

    I'll avoid discussing how much the design decision there makes me cringe... it's very denormalized.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Here's another way:

    WHERE LEN(LastName) - 1 = LEN(REPLACE(LastName, '/', ''))


    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

  • Thank you both for the help, dwain I've used your example for getting the column information. It works great however it returns rows that begin with a '/' too which isn't exactly what I want. Is there a way to query only the last names with at least one character (letter or number) before the '/'? For example, last names like 'e/johnson' but not names like '/johnson'.

  • AND SUBSTRING(LastName,1,1)<>'/' ---??

  • gravitysucks (10/5/2012)


    AND SUBSTRING(LastName,1,1)<>'/' ---??

    Yes or:

    AND LEFT(LTRIM(LastName), 1) <> '/'

    The LTRIM is in case there are blanks only leading up to the slash.


    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

  • CELKO (10/5/2012)


    You are not just new to the forums, but have never read anything about RDBMS.

    Backoff the new guy, Joe. You don't know if he created the denormalized data or not. Even if he did, that's not teaching and there's no need for that kind of comment.

    What you have is an attempt at a variant record in several different 1950's threu 1970's file systems.

    BWAA-HAAA!!! You always say stuff like that but aren't you still using a 1950s push-stack to convert Adjacency Lists to Nested Sets?

    If you do nto care about being a good SQL programmer, there are several kludges for splitting this string into separate values.

    Once again, you're totally out of line with such a comment. Good programmers will, in fact, use some of the "kludges" to normalize the data if they're allowed and to simply use the data if they're not. Heh... and it's not nearly as bad as the push-stack kludge. πŸ˜‰

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thank you all for the samples, dwain.c I utilized your excerpt and it worked for me. I appreciate everyone's help!

  • SELECT lastname

    FROM HR.Employees

    WHERE

    lastname LIKE '%[/]%' AND

    lastname NOT LIKE '%[/]%[/]%'

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • ScottPletcher (10/8/2012)


    SELECT lastname

    FROM HR.Employees

    WHERE

    lastname LIKE '%[/]%' AND

    lastname NOT LIKE '%[/]%[/]%'

    Still have to add the line that removes / in the first character. I also believe you would want to remove any where / was the last character...

    And Left(Ltrim(LastName), 1) <> '/'

    And Right(Rtrim(LastName),1) <> '/'

    I'm curious if this would not be a good option from an execution plan perspective? Are there better / faster ways of completing this?

    [font="Arial"]β€œAny fool can know. The point is to understand.”
    - Albert Einstein

    "DOH!"
    - Homer Simpson[/font]

  • jarid.lawson (10/8/2012)


    ScottPletcher (10/8/2012)


    SELECT lastname

    FROM HR.Employees

    WHERE

    lastname LIKE '%[/]%' AND

    lastname NOT LIKE '%[/]%[/]%'

    Still have to add the line that removes / in the first character. I also believe you would want to remove any where / was the last character...

    And Left(Ltrim(LastName), 1) <> '/'

    And Right(Rtrim(LastName),1) <> '/'

    I'm curious if this would not be a good option from an execution plan perspective? Are there better / faster ways of completing this?

    Oops, I didn't initially read down far enough to see the new requirement of not a leading /:

    SELECT lastname

    FROM HR.Employees

    WHERE

    lastname LIKE '[^/]%[/]%' AND

    lastname NOT LIKE '[^/]%[/]%[/]%'

    [/quote]

    In theory this could potentially be faster with a covering index on lastname (probably likely, if you plan on running this query very often), whereas the queries using REPLACE(...) or any other function on the lastname column would require a full table scan.

    That is, "lastname [NOT] like" is SARGable but "<function>(... lastname ...)" is not.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 11 posts - 1 through 10 (of 10 total)

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