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.
| Author |
Topic |
|
browserfiles
Starting Member
2 Posts |
Posted - 2011-11-08 : 02:28:58
|
hello to sql gurus, i am quite a newbie to t-sql so please be gentle. i have been at this for days trying to modify a split string function i found over the web but still no progress.Here is the background:I have a string data delimited by a comma, each split is grouped by another delimiter, an example:1^1~1,1^1~2,1^1~3,1^1~4I am using this format to populate a field that corresponds to a group of profile (an example that illustrates my objective):1. the first number in the each split designates the favorites category (sports, music, favorite actors, etc.) which is a PK in a favorites category table2. the second number in each split is the type of favorite in each category (indoor sports, outdoor sports, water sports, jazz, rock, ballad) also a PK in a favorite type table3. the last number in the split is the name of each favorites type (basketball, volleyball, snowboarding, Sean Connery, etc) on a favorite names tableMy objective is to parse this long line of text and generate and generate the 'text-version' of the string such as:sports:outdoor-basketball,sports:outdoor-volleyball,music:rock-wewillrockyou,music:mellow-heysoulsisteri have tried modifying a script i saw on one of the threads SplitString function - i am using this with one of my fields but modifying this script seems to be a nightmare for me. I was planning to modify the SplitString function to take in four parameters (delimiter 1 to 3 and the original string) and do the parsing in a recursive manner (if possible):dbo.SplitString(',', '^', '~', '1^1~1,1^1~2,1^1~3,1^1~4') To any good hearted sql pro out there, please help.If anybody is interested to see what i have been working on heere is the code:FUNCTION [dbo].[SplitString]( @Delimiter varchar(1), @List varchar(8000) ) RETURNS @TableOfValues table ( RowID smallint IDENTITY(1,1), --[Value] varchar(50) [Value] int ) ASBEGIN DECLARE @LenString int WHILE len( @List ) > 0 BEGIN SELECT @LenString = (CASE charindex( @Delimiter, @List ) WHEN 0 THEN len( @List ) ELSE ( charindex( @Delimiter, @List ) -1 ) END ) INSERT INTO @TableOfValues SELECT SUBSTRING(@List, 1, @LenString) SELECT @List = (CASE ( len( @List ) - @LenString ) WHEN 0 THEN '' ELSE right( @List, len( @List ) - @LenString - 1 ) END ) END RETURN END |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-11-08 : 02:52:26
|
[code]select *, dbo.fnParseString(-1, ':', Data), dbo.fnParseString(-1, '-', dbo.fnParseString(-2, ':', Data)), dbo.fnParseString(-2, '-', Data)from dbo.fnParseList(',', 'sports:outdoor-basketball,sports:outdoor-volleyball,music:rock-wewillrockyou,music:mellow-heysoulsister')[/code]fnParseString & fnParseList is from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-08 : 03:14:13
|
| I didnt understand why you're following such a design for sending data. instead of using delimited strings with all kinds of delimiters it would be much better if you had passed values as a xml with node names representing what data they contain. that would improve clarity and would allows others also to understand what data it contains and where it should get populated.By making it XML, you can also make use of new xml methods available from 2005 onwards like nodes(),query() etc to shred the component on to different fields of table which would perform much better than parsing logic using udf. You even have ability to add XML indexes if you want to further speed up the search.for example in above case equivalent xml would be something like<Favourites> <Category>sports</Category> <Type>indoorsports</Type> <Typename>basketball</Typename></Favourites>...------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
browserfiles
Starting Member
2 Posts |
Posted - 2011-11-08 : 03:44:32
|
quote: Originally posted by visakh16 I didnt understand why you're following such a design for sending data. instead of using delimited strings with all kinds of delimiters it would be much better if you had passed values as a xml with node names representing what data they contain. that would improve clarity and would allows others also to understand what data it contains and where it should get populated.By making it XML, you can also make use of new xml methods available from 2005 onwards like nodes(),query() etc to shred the component on to different fields of table which would perform much better than parsing logic using udf. You even have ability to add XML indexes if you want to further speed up the search...
Thank you for your reply visakh16. Alas I am not able to use XML as it is not within my expertise along with time constraints for the learning curve. I use this structure so i can easily convert them into page controls (asp.net listbox, checkboxes, etc) as I am more of a GUI guy.In any event, I will try out khtan's reference on fnParseString and fnParseList functions. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-08 : 03:51:54
|
| ok. but just understand that you're making it difficult using above design both calrity and functionality wise to split up values and then do your processing.i would certainly suggest you to understand xml and go for it if at all possible in above scenario------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|