Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

T-sql select - like Expand / Collapse
Author
Message
Posted Thursday, October 4, 2012 4:05 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 15, 2013 12:24 PM
Points: 9, 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.
Post #1368762
Posted Thursday, October 4, 2012 4:09 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, October 18, 2014 4:43 AM
Points: 5,401, Visits: 7,512
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
Post #1368764
Posted Thursday, October 4, 2012 6:21 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!
Post #1368788
Posted Friday, October 5, 2012 3:16 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 15, 2013 12:24 PM
Points: 9, 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'.
Post #1369328
Posted Friday, October 5, 2012 4:40 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 2:57 PM
Points: 53, Visits: 161
AND SUBSTRING(LastName,1,1)<>'/' ---??
Post #1369343
Posted Friday, October 5, 2012 5:50 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 6:20 PM
Points: 1,945, Visits: 3,067
>> 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. <,

You are not just new to the forums, but have never read anything about RDBMS. A column is by definition, a scalar value. This is part of First Normal Form (1NF) and it is usually covered in the first 3-4 chapters of any book on the topic. What you have is an attempt at a variant record in several different 1950's threu 1970's file systems.

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

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


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1369351
Posted Friday, October 5, 2012 8:07 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!
Post #1369361
Posted Saturday, October 6, 2012 1:17 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:08 AM
Points: 35,366, Visits: 31,901
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1369456
Posted Monday, October 8, 2012 9:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 15, 2013 12:24 PM
Points: 9, Visits: 40
Thank you all for the samples, dwain.c I utilized your excerpt and it worked for me. I appreciate everyone's help!
Post #1369878
Posted Monday, October 8, 2012 10:26 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 3:07 PM
Points: 2,194, Visits: 3,304

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



SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #1369941
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse