[Show all top banners]

slipknot_IW
Replies to this thread:

More by slipknot_IW
What people are reading
Subscribers
:: Subscribe
Back to: Computer/IT Refresh page to view new replies
 SQL Server Help.
[VIEWED 3665 TIMES]
SAVE! for ease of future access.
Posted on 01-17-06 4:22 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hello all, I need help on solving this problem.

I have a table "Table A" with ID and Items Fields. ID is unique. Items Filed contains Part numbers with ';' dilimited.

ID | Items
1 | AFF-12D; ACC-15S;
2 | ACC-15S;
2 | KXX-1129;

How can I select rows with ACC-155. How can i do it?

Thanks in advance.
 
Posted on 01-17-06 4:28 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

You can do something like:

Select * from A where Items like '%ACC-155%'
 
Posted on 01-17-06 4:36 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

yupe.... that is the best way..
 
Posted on 01-17-06 4:38 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Metal and Roll... Thanks UZZO.
 
Posted on 01-17-06 5:45 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Well, there are a couple of problems.

#1. Putting a delimited list in a field is not a good database design practice. In fact, to be honest, it is a BAD design. It doesn't even qualify the 1st normal form of the Relational Database Theory. You gotta break it down into multiple tables. Example:

TABLE PART:

ID PART_DESC ... ...
1 My Part 1
2 My Part 2 ... ...
3 My Part 3 ... ...

TABLE PART_CODE:

PART_ID CODE
1 AFF-12D
1 ACC-15S
2 ACC-15S
3 KXX-1129

#2. The SQL uzzo suggested has a problem that may not appear at first, but will surface sooner or later. (and your pager will vibrate at 2AM -;)). The problem is that:

Select * from A where Items like '%ACC-155%'

Will not only select ACC-155, but also:

mACC-155n

where:
m = one or more (any) characters.
n = one or more (any) characters.

Examples:
KAAC-1558
RAAC-155z9
..and all other infinite possibilities.

Again, it may not cause problems now because you don't have enough data, but you should think ahead...

So the solution is simple if you redesign your database the way I suggested (and that's the way it should be). Your SQL will simply look like:

Select * from PART, PART_CODE where PART.ID = PART_CODE.PART_ID AND PART_CODE.CODE = 'ACC-155'

That will work 100% of the time.

But if you must keep (please don't!) your existing design, then at least rewrite your SQL as this:

Select * from A where (Items = 'ACC-155') OR (Items like 'ACC-155;%') OR (Items like '%;ACC-155;%') OR (Items like '%;ACC-155')

(That's to make sure that you don't select a wrong item just because part of the code matches, yet take care of all possible cases how a valid item code of 'ACC-155' may exist in the field.)

I hope you can see the difference. See it gets ugly and error prone. That's why you need to follow the relational theory when you do the design and normalize your data. It's there for a reason ni!
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 365 days
Recommended Popular Threads Controvertial Threads
शीर्षक जे पनि हुन सक्छ।
डीभी परेन भने खुसि हुनु होस् ! अमेरिकामाधेरै का श्रीमती अर्कैसँग पोइला गएका छन् !
What are your first memories of when Nepal Television Began?
Sajha Poll: नेपालका सबैभन्दा आकर्षक महिला को हुन्?
ChatSansar.com Naya Nepal Chat
NRN card pros and cons?
TPS Re-registration case still pending ..
Basnet or Basnyat ??
TPS Re-registration
निगुरो थाहा छ ??
Nas and The Bokas: Coming to a Night Club near you
Toilet paper or water?
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
अमेरिकामा छोरा हराएको सूचना
ढ्याउ गर्दा दसैँको खसी गनाउच
Sajha Poll: Who is your favorite Nepali actress?
Problems of Nepalese students in US
nrn citizenship
TPS EAD auto extended to June 2025 or just TPS?
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
Nas and The Bokas: Coming to a Night Club near you
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters