Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Split String with row terminator Rate Topic Display Mode Topic Options
Author
 Message
 Posted Saturday, March 15, 2014 4:01 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Wednesday, April 9, 2014 11:31 AM Points: 175, Visits: 351
 Hi,i read the below link article and and use the below function for splitting the string. I love the Jeff moden's split string and below code has modified by Aaron on top of the jeff's function.[url=http://www.sqlperformance.com/2012/07/t-sql-queries/split-strings][/url]`CREATE FUNCTION dbo.SplitStrings_Moden( @List NVARCHAR(MAX), @Delimiter NVARCHAR(255))RETURNS TABLEWITH SCHEMABINDING ASRETURN WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1), E2(N) AS (SELECT 1 FROM E1 a, E1 b), E4(N) AS (SELECT 1 FROM E2 a, E2 b), E42(N) AS (SELECT 1 FROM E4 a, E2 b), cteTally(N) AS (SELECT 0 UNION ALL SELECT TOP (DATALENGTH(ISNULL(@List,1))) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E42), cteStart(N1) AS (SELECT t.N+1 FROM cteTally t WHERE (SUBSTRING(@List,t.N,1) = @Delimiter OR t.N = 0)) SELECT Item = SUBSTRING(@List, s.N1, ISNULL(NULLIF(CHARINDEX(@Delimiter,@List,s.N1),0)-s.N1,8000)) FROM cteStart s;`I literally need to have passing string as MAX which is excellent on the above function. Basically i am reading text file from my front end and passing the entire text to back end for splitting with delimiter and ROWTERMINATOR [backward slash n]please don't ask about why am doing this as i can use bulk insert for moving the data from text file to my db table. because i cannot have my text file on the database server to copy the record with bulk insert feature.my sample data which needs to be split with delimiter and ROWTERMINATOR [backward slash n]`'2D5E558D4B5A3D4F962DA5051EE364BE06CF37A3A5@Server.com|user1@domain1.comE52F650C53A275488552FFD49F98E9A6BEA1262E@Server.com|user2@domain2.com4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user3@domain3.com4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user4@domain4.com4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com |user5@domain5.com'`on the above example delimiter is '|' and ROWTERMINATOR is [backward slash n]Expected output as two columns with 5 rowsplease execute the below query to see how the expected output will be ` select '2D5E558D4B5A3D4F962DA5051EE364BE06CF37A3A5@Server.com' as server, 'user1@domain1.com' as domain union allselect 'E52F650C53A275488552FFD49F98E9A6BEA1262E@Server.com' as server, 'user2@domain2.com' as domain union allselect '4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com' as server, 'user3@domain3.com' as domain union allselect '4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com'as server,'user4@domain4.com' as domain union allselect '4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com'as server,'user5@domain5.com'` is there any way to achieve this. is yes please suggest me with some sample Thanks.
Post #1551524
 Posted Sunday, March 16, 2014 2:27 AM
 SSCrazy Group: General Forum Members Last Login: Yesterday @ 10:21 AM Points: 2,128, Visits: 5,561
 I don't think that you need to work with a user defined function. Take a look at the code bellow. Is that what you need? Notice that I added a select with the union all. You didn't have it in your example, but without it, you'll have a run time error (that’s assuming that you wanted to select this data).`declare @MyString varchar(max)set @MyString = '2D5E558D4B5A3D4F962DA5051EE364BE06CF37A3A5@Server.com|user1@domain1.comE52F650C53A275488552FFD49F98E9A6BEA1262E@Server.com|user2@domain2.com4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user3@domain3.com4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user4@domain4.com4fd70c47.4d600e0a.0a7b.ffff87e1@Server.com |user5@domain5.com'select @MyString = 'SELECT ''' + replace(replace(@MyString,'|', ''' as server, '''),'',''' as domain union allselect ''') + ''' as domain'select @MyStringexec(@MyString)`Adi --------------------------------------------------------------To know how to ask questions and increase the chances of getting asnwers:http://www.sqlservercentral.com/articles/Best+Practices/61537/For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1551546
 Posted Sunday, March 16, 2014 7:12 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Wednesday, April 9, 2014 11:31 AM Points: 175, Visits: 351
 Hi Adi,Thanks for your reply and sample. I just gave the sample data. Here is the table structure which i will import the query output to the table.`Create table DataFiles(IdData int identity(1,1) primary key, server nvarchar(4000),domain nvarchar(50),ReceivedFilescount int,DateReceived date,company varchar(50),ExpectedFilesCount int)`Sample data`'2D5E558D4B5A3D4F962DA5051EE364BE06CF37A3A5@Server.com|user1@domain1.com|0|2014-02-05|Microsoft|100E52F650C53A275488552FFD49F98E9A6BEA1262E@Server.com|user2@domain2.com|1|2014-03-05|Samsumg|1204fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user3@domain3.com|2|2014-01-05|Nokia|1394fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user4@domain4.com|3|2014-02-08|HTC|1494fd70c47.4d600e0a.0a7b.ffff87e1@Server.com |user5@domain5.com|4|2014-02-13|Paypal|129'`I should have been given whole data. my mistake. sorry about that. Also please note that there will be an addition to this data in future as well as on the new columns on my table.So on your sample you have user Replace function for making the column name, and constructing the column name with select statement. if there is more column then construction will be cumbersome.Is there any other way to achieve this or how to simplify your query ?
Post #1551560
 Posted Sunday, March 16, 2014 11:35 AM
 SSCrazy Group: General Forum Members Last Login: Yesterday @ 10:21 AM Points: 2,128, Visits: 5,561
 Ectualy you don't need to name the columns in the select statement, so you can still use 2 replace statements and it will work. You can check the code bellow that inserts the data into the table`Create table DataFiles(IdData int identity(1,1) primary key, server nvarchar(4000),domain nvarchar(50),ReceivedFilescount int,DateReceived date,company varchar(50),ExpectedFilesCount int)declare @SQL varchar(max)declare @MyString varchar(max) ='2D5E558D4B5A3D4F962DA5051EE364BE06CF37A3A5@Server.com|user1@domain1.com|0|2014-02-05|Microsoft|100E52F650C53A275488552FFD49F98E9A6BEA1262E@Server.com|user2@domain2.com|1|2014-03-05|Samsumg|1204fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user3@domain3.com|2|2014-01-05|Nokia|1394fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user4@domain4.com|3|2014-02-08|HTC|1494fd70c47.4d600e0a.0a7b.ffff87e1@Server.com |user5@domain5.com|4|2014-02-13|Paypal|129'select @MyString = 'SELECT ''' +replace(replace(@MyString,'|',''','''),'',''' union select ''') + ''''select @MyStringexec (@MyString)set @SQL = 'INSERT INTO DataFiles(server, domain, ReceivedFilescount, DateReceived, company, ExpectedFilesCount)' + @MyStringexec (@SQL)select * from DataFiles` Although the code works, I think that you should look on working with bulk insert statement or BCP. In my opinion it is more suitable then using my code.Adi --------------------------------------------------------------To know how to ask questions and increase the chances of getting asnwers:http://www.sqlservercentral.com/articles/Best+Practices/61537/For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1551576
 Posted Sunday, March 16, 2014 3:13 PM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 12:24 PM Points: 35,610, Visits: 32,203
Post #1551597
 Posted Sunday, March 16, 2014 3:34 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Wednesday, April 9, 2014 11:31 AM Points: 175, Visits: 351
 Hi Adi, thanks for your reply, sample and appreciate your time on this post.Hi Jeff,Thanks for your replySo from your statement it's not good to use the function which i mentioned because of the performance impact. I understand. How about Adi's Suggestion. Logically it works with my data. is there any performance impact on this suggestion. Please suggest me
Post #1551602
 Posted Sunday, March 16, 2014 3:57 PM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 12:24 PM Points: 35,610, Visits: 32,203
Post #1551606
 Posted Sunday, March 16, 2014 4:42 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Wednesday, April 9, 2014 11:31 AM Points: 175, Visits: 351
Post #1551608
 Posted Sunday, March 16, 2014 5:06 PM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 12:24 PM Points: 35,610, Visits: 32,203
 I don't know how many "rows" will be in each string that you get. If it's more than a couple hundred, Adi's good code is going to get real slow. If it's more than that, it will eventually fail because you can only have a certain number of SELECTs per query. Adi's code is also subject to SQL Injection attacks.You can make his code a little faster and a lot safer if you make the small changes that I've included below to the one section of code that it represents.`select @MyString = 'SELECT ''' +replace(replace(@MyString,'|',''','''),'',''' union ALL select ''') + ''''WHERE @MyString NOT LIKE '%[^-a-zA-Z0-9@|.'+CHAR(10)+CHAR(13)+']%'`I agree with Adi... this is a job better done by BULK INSERT or BCP. BCP won't have the same server-to-server trust problems as BULK INSERT so that may be the way to go here. If even that is not allowed, then a well written CLR would serve you better than any manifestation of the DelimitedSplit8K function or Adi's good code. --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 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1551611
 Posted Sunday, March 16, 2014 5:59 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Wednesday, April 9, 2014 11:31 AM Points: 175, Visits: 351
 Hi Jeff,Thank you so much for your wonderful time on this post and it really helped me. i tried to incorporate your changes on Adi's query and getting error. It doesn't say any clue about the issue. `declare @SQL varchar(max)declare @MyString varchar(max) ='2D5E558D4B5A3D4F962DA5051EE364BE06CF37A3A5@Server.com|user1@domain1.com|0|2014-02-05|Microsoft|100E52F650C53A275488552FFD49F98E9A6BEA1262E@Server.com|user2@domain2.com|1|2014-03-05|Samsumg|1204fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user3@domain3.com|2|2014-01-05|Nokia|1394fd70c47.4d600e0a.0a7b.ffff87e1@Server.com|user4@domain4.com|3|2014-02-08|HTC|1494fd70c47.4d600e0a.0a7b.ffff87e1@Server.com |user5@domain5.com|4|2014-02-13|Paypal|129'select @MyString = 'SELECT ''' +replace(replace(@MyString,'|',''','''),'',''' union ALL select ''') + ''''WHERE @MyString NOT LIKE '%[^-a-zA-Z0-9@|.'+CHAR(10)+CHAR(13)+']%'--select @MyString--exec (@MyString)set @SQL = 'INSERT INTO DataFiles(server, domain, ReceivedFilescount, DateReceived, company, ExpectedFilesCount)' + @MyStringexec (@SQL)`Any suggestion please
Post #1551613

 Permissions