Pulling data from a column of multiple items that are comma delimited
Hi all,
I have no clue if this can be done or not, I have a database of autoparts,
one table is for cars ( that each part would fit ) I understand that to normalize
this you would want one car per entry in the cars table but Im getting this
data from a customer and he has multiple cars (example: 240SX,300ZX,PATHFINDER,MAXIMA)
set up in the car table. Meaning one part ( from the parts table) would fit
all those cars.
I need some simple/basic instructions on how I would parse or manipulate
the data or even if I can using an SQL statement ( the database is access
) to match it up with the part if a user searches for a single car.
I guess my question is ... how do you pull a single name from a list of comma
delimited items and display only a single item back to the user ?
( as you can see im not even sure how to ask the question ) any help
or even getting me on the right track would be great .....
Thanks
Mike
[1013 byte] By [
Mike] at [2007-11-9 21:11:00]

# 1 Re: Pulling data from a column of multiple items that are comma delimited
Mike the easy answer is to use the like operator and % signs around the search
string
So, your query would look like
SELECT Field1, Field2, ...
FROM tblName
WHERE SupportedCars Like '%MAXIMA%'
Anohter option would be to use the instr function such as the following:
SELECT Field1, Field2, ...
FROM tblName
HAVING Instr(SupportedCars,'MAXIMA',1)>0
*Order for instr criteria may be off...
Q*Bert
"Mike" <mike333@cox.net> wrote:
>
>Hi all,
>
>I have no clue if this can be done or not, I have a database of autoparts,
>one table is for cars ( that each part would fit ) I understand that to
normalize
>this you would want one car per entry in the cars table but Im getting this
>data from a customer and he has multiple cars (example: 240SX,300ZX,PATHFINDER,MAXIMA)
>set up in the car table. Meaning one part ( from the parts table) would
fit
>all those cars.
>
>I need some simple/basic instructions on how I would parse or manipulate
>the data or even if I can using an SQL statement ( the database is access
>) to match it up with the part if a user searches for a single car.
>
>
>I guess my question is ... how do you pull a single name from a list of
comma
>delimited items and display only a single item back to the user ?
>
>( as you can see im not even sure how to ask the question ) any help
>or even getting me on the right track would be great .....
>
>Thanks
>
>Mike
Q*bert at 2007-11-11 23:51:59 >

# 2 Re: Pulling data from a column of multiple items that are comma delimited
1st thanks for the responce Luke, Ok I see how that would return 1 car, but,
what if I wanted to allow the user of this to choose from all the cars in
a drop down menu listed one per line ? Here is an example of what Im using
this for, http://www.loveinsandiego.com/all_search.asp ( please be clean
with the data if you fool around with it ) How I got it to this point was
to take every instance where there were multiple cars in the carName column
( 240SX,Maxima,Pathfinder) and work them into a new record which created
yet another problem with a duplicate part number ( if that makes sence to
you.
To restate the what I need to do would look something like this .....
I have 3 tables
PARTS:
PartID
CarID
CategoryID
MotorType
PartNumber
PartDescription
BigImage
SmallImage
CATEGORY:
CategoryID
CategoryName
Car:
CarID
CarName
And I can get these to work and run some nice querys from them.
Here is the problem. In the Cars Table the CarName information is stored
in comma delimited fashion.
Example one record would have this : 240SX
Then another record would have: 240SX, ALTIMA
And another would have something like: 300ZX, SENTRA, 240SX
and these match up to the PartID in the Parts table.
This is set up that one part ( from the parts table ) would fit multiple
cars.
What I want to be able to do with this is allow a user to select a car from
a list menu populated with only 1 name at a time and then maybe have some
information come up saying something like
ALSO FITS: and returning the complete list of cars that this part would fit.
And also allow the customer of the database when entering information about
a new car part or on an update page. to enter multiple cars that the part
would fit.
Im thinking that the Cars table needs to be split out into another table
with each car listed as a single entry.
Example:
240SX
ALTIMA
300ZX
SENTRA
So I guess I stated the question wrong in the first place .... what I need
to do is pull the info ( all cars listed ) from a comma delimited column
so that I can use that list to populate the list menu or if you have a better
way to populate the list menu let me know ....
Thank you
"Q*bert" <luke_Davis_76@hotmail.com> wrote:
>
>Mike the easy answer is to use the like operator and % signs around the
search
>string
>
>So, your query would look like
>
>SELECT Field1, Field2, ...
>FROM tblName
>WHERE SupportedCars Like '%MAXIMA%'
>
>Anohter option would be to use the instr function such as the following:
>
>SELECT Field1, Field2, ...
>FROM tblName
>HAVING Instr(SupportedCars,'MAXIMA',1)>0
>
>*Order for instr criteria may be off...
>
>
>Q*Bert
>
>"Mike" <mike333@cox.net> wrote:
>>
>>Hi all,
>>
>>I have no clue if this can be done or not, I have a database of autoparts,
>>one table is for cars ( that each part would fit ) I understand that to
>normalize
>>this you would want one car per entry in the cars table but Im getting
this
>>data from a customer and he has multiple cars (example: 240SX,300ZX,PATHFINDER,MAXIMA)
>>set up in the car table. Meaning one part ( from the parts table) would
>fit
>>all those cars.
>>
>>I need some simple/basic instructions on how I would parse or manipulate
>>the data or even if I can using an SQL statement ( the database is access
>>) to match it up with the part if a user searches for a single car.
>>
>>
>>I guess my question is ... how do you pull a single name from a list of
>comma
>>delimited items and display only a single item back to the user ?
>>
>>( as you can see im not even sure how to ask the question ) any help
>>or even getting me on the right track would be great .....
>>
>>Thanks
>>
>>Mike
>
# 3 Re: Pulling data from a column of multiple items that are comma delimited
hope this can help u out... Use ADO... It looks bad on this text mode. E-mail
me, if u need source code.. The source code is shown as below
bool InsertData(char ID[6], char ItemName[50], char HyperLink[150], char
Attr[150])
{
/* ******************************************** */
/* SQL Server Setups */
/* ******************************************** */
char ServerName[] = "MySQLServer";
char UID[] = "MyID";
char Passw[] = "MyPassword";
char SQLItem[] = "Driver=SQL Server; server=MySQLServer; UID=MyID; PWD=MyPassword;
database=oadb";
int PassLength, k;
char SQLCommand_01[350],;
char ItemNo[5], Shortcut[100];
size_t n, i;
PassLength = 0;
n = i = 0;
// Select Recoders that need to Be Modified
sprintf(SQLCommand_01,"select OI_Index from OAMenu_ItemList where OI_ItemName
= '%s' and OI_HypeLink = '%s'", ItemName, HyperLink);
// Select Shortcut which in user's record
sprintf(SQLCommand_02,"select OU_UserID, OU_Shortcut from OAMenu_UserInfo
where OU_UserID = '%s'", ID);
try
{
::CoInitialize(NULL);
/************************************************************/
/* Create connection for SQLCommand_01 */
/************************************************************/
_ConnectionPtr pConn_01(__uuidof(Connection));// Create a connection object.
// Open the connection.
pConn_01->Open(SQLItem, UID, Passw, PassLength);
// Opening a recordset using an existing connection.
_RecordsetPtr pRS_01(__uuidof(Recordset)); // Create a recordset object
// Get item number from OAMenu_ItemList
pRS_01->Open(SQLCommand_01, pConn_01.GetInterfacePtr(), adOpenStatic, adLockOptimistic,
adCmdUnknown);
/************************************************************/
/* Create connection for SQLCommand_02 */
/************************************************************/
_ConnectionPtr pConn_02(__uuidof(Connection));// Create a connection object.
// Open the connection.
pConn_02->Open(SQLItem, UID, Passw, PassLength);
// Opening a recordset using an existing connection.
_RecordsetPtr pRS_02(__uuidof(Recordset)); // Create a recordset object
/************************************************************/
/* Create connection for SQLCommand_03 and SQLCommand_04 */
/************************************************************/
_ConnectionPtr pConn_03(__uuidof(Connection));// Create a connection object.
// Open the connection.
pConn_03->Open(SQLItem, UID, Passw, PassLength);
// Opening a recordset using an existing connection.
_RecordsetPtr pRS_03(__uuidof(Recordset)); // Create a recordset object
/************************************************************/
/* Create connection for the following SQLCommand_01 */
/************************************************************/
_ConnectionPtr pConn_04(__uuidof(Connection));// Create a connection object.
// Open the connection.
pConn_04->Open(SQLItem, UID, Passw, PassLength);
// Opening a recordset using an existing connection.
_RecordsetPtr pRS_04(__uuidof(Recordset)); // Create a recordset object
//enumerate every record.
if(pRS_01->adoEOF == false) // This shortcut can be found in OAMenu_ItemList
{
strcpy(ItemNo, StrTrimRight( UC _bstr_t(RsITEM(pRS_01,0L))));
i = strlen(ItemNo);
// Get OU_Shortcut from OAMenu_UserInfo by employee ID
pRS_02->Open(SQLCommand_02, pConn_02.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
if(pRS_02->adoEOF == false) // This empolyees' record exists in the database
"OAMenu_UserInfo"
{
strcpy(Shortcut, StrTrimRight( UC _bstr_t(RsITEM(pRS_02,1L))));
n = strlen(Shortcut);
for(k = 0; k < i; k++)
{
Shortcut[n] = ItemNo[k];
n++;
}
Shortcut[n] = ',';
Shortcut[n+1] = NULL;
// Update user's shortcut
sprintf(SQLCommand_03,"Update OAMenu_UserInfo set OU_Shortcut = '%s',
OU_Flag = 0 where OU_UserID ='%s'", Shortcut, ID);
pRS_03->Open(SQLCommand_03, pConn_03.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
}
else // This empolyees' record doesn't exist in the database "OAMenu_UserInfo"
{
n = 0;
for(k = 0; k < i; k++)
{
Shortcut[n] = ItemNo[k];
n++;
}
Shortcut[n] = ',';
Shortcut[n+1] = NULL;
// Update user's shortcut
sprintf(SQLCommand_05,"insert into OAMenu_UserInfo values ('%s','','%s','',0)",
ID, Shortcut);
pRS_03->Open(SQLCommand_05, pConn_03.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
}
}
else // This Shortcut is not in ITRI.shc
{
// Insert this item into OAMenu_ItemList
sprintf(SQLCommand_04, "insert into OAMenu_ItemList values(1,'Others','','%s','%s','%s','','')",
ItemName, HyperLink, Attr);
pRS_03->Open(SQLCommand_04, pConn_03.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
// Retry to get item number
pRS_04->Open(SQLCommand_01, pConn_04.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
strcpy(ItemNo, StrTrimRight( UC _bstr_t(RsITEM(pRS_04,0L))));
i = strlen(ItemNo);
// Get OU_Shortcut from OAMenu_UserInfo by employee ID
pRS_02->Open(SQLCommand_02, pConn_02.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
if(pRS_02->adoEOF == false) // This empolyees' record exists in the database
"OAMenu_UserInfo"
{
strcpy(Shortcut, StrTrimRight( UC _bstr_t(RsITEM(pRS_02,1L))));
n = strlen(Shortcut);
for(k = 0; k < i; k++)
{
Shortcut[n] = ItemNo[k];
n++;
}
Shortcut[n] = ',';
Shortcut[n+1] = NULL;
// Upadte OAMenu_UserInfo
sprintf(SQLCommand_03,"Update OAMenu_UserInfo set OU_Shortcut = '%s',
OU_Flag = 0 where OU_UserID ='%s'", Shortcut, ID);
pRS_03->Open(SQLCommand_03, pConn_03.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
}
else // This empolyees' record doesn't exists in the database "OAMenu_UserInfo"
{
n = 0;
for(k = 0; k < i; k++)
{
Shortcut[n] = ItemNo[k];
n++;
}
Shortcut[n] = ',';
Shortcut[n+1] = NULL;
// Upadte OAMenu_UserInfo
sprintf(SQLCommand_05,"insert into OAMenu_UserInfo values ('%s','','%s','',0)",
ID, Shortcut);
pRS_03->Open(SQLCommand_05, pConn_03.GetInterfacePtr(), adOpenStatic,
adLockOptimistic, adCmdUnknown);
}
}
pConn_01->Close();
pConn_02->Close();
pConn_03->Close();
pConn_04->Close();
return true;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bs = _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg:
") + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") + _bstr_t(e.Description());
::CoInitialize( NULL );
MessageBox(0,bs,bstrSource, MB_OK);
return false;
}
}
Paul at 2007-11-11 23:54:06 >
