SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-sql select - like


T-sql select - like

Author
Message
ggarcia 73023
ggarcia 73023
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 40
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.
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9023 Visits: 7660
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7767 Visits: 6431
Here's another way:


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





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
ggarcia 73023
ggarcia 73023
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 40
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'.
gravitysucks
gravitysucks
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 232
AND SUBSTRING(LastName,1,1)<>'/' ---??
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7767 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91218 Visits: 41151
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
ggarcia 73023
ggarcia 73023
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 40
Thank you all for the samples, dwain.c I utilized your excerpt and it worked for me. I appreciate everyone's help!
ScottPletcher
ScottPletcher
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8398 Visits: 7163

SELECT lastname
FROM HR.Employees
WHERE
lastname LIKE '%[/]%' AND
lastname NOT LIKE '%[/]%[/]%'



SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search