Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Split delimited data

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-05-23 : 08:07:01
KGarf writes "I am using SQL 2000 and my operating system is Win NT.

I have a database field called Reasons that contains one or more reasons. If there is more than one reason the reasons are separated by an underscore character like so:

NOEXP_LATE

What I need to do in a stored procedure is to split this field up into however number of reasons there are.

For example:

ID 1
Reasons LATE_NOADD

Would become
ID 1
Reason1 LATE
Reason2 NOADD

AND

ID 2
Reasons NOEXP_NOADD_LATE_NOGCG

Would become
ID 2
Reason1 NOEXP
Reason2 NOADD
Reason3 LATE
Reason4 NOGCG

I'm sure i'd have to do a loop to check the number of occurrences of the underscore character and then split the string from left to right and increment each reason by 1.

I only need to split this field into the number of reasons in a stored procedure. I would appreciate your help on this one. Many thanks."

nr
SQLTeam MVY

12543 Posts

Posted - 2005-05-23 : 08:17:40
see
http://www.mindsdoor.net/SQLTsql/ParseCSVString.html

Loop through each row calling that function which will return a resultset of entries.

You could also use
http://www.mindsdoor.net/SQLTsql/f_GetEntryDelimiitted.html
and loop through the field number.




==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-05-23 : 08:51:41
As always, consider the radical idea of normalizing your database as well.

http://www.datamodel.org/NormalizationRules.html

You should never store multiple values in the same column in a single row. Break it out into a related table.

- Jeff
Go to Top of Page
   

- Advertisement -