In answer to yuor questions1) Can this be done in SQL? YES2) Do I have to write a UDF or can it be done in T-SQL? If you need the proccess to DO something you can't write it as a UDF -- you could write it as a stored proc easily.Here's a *direct* (SQL so horrible that dead database admins are revolving in their graves right now) translation of your vb code.DECLARE @cnt INT SET @cnt = 0DECLARE @H_comment NVARCHAR(MAX) SET @H_comment = 'This is a comment more than 50 characters long. This will be split into sets of 50 characters. There is no reason to do this in SQL, not is this a good implementation of such a way to do so. Also @cnt is declared but never used.'DECLARE @h_len1 INT SET @h_len1 = LEN(@H_comment)DECLARE @x INT SET @x = 1DECLARE @doIt FLOAT SET @doIt = @h_len1 % 50DECLARE @loopIt FLOAT SET @loopIt = @doItDECLARE @S_comment NVARCHAR(50) SET @S_comment = NULLSET @doIt = @h_len1 % 50IF @doIt > 0 SET @loopIt = @loopIt + 1WHILE @loopIt > 0 BEGIN SET @S_comment = SUBSTRING(@H_comment, @x, 50) IF @S_comment > '' BEGIN SET @cnt = @cnt + 1 -- Do write_comment here (this could be a proc) PRINT @S_comment END SET @loopIt = @loopIt - 1 SET @x = @x + 50END
Results = This is a comment more than 50 characters long. This will be split into sets of 50 characters. There is no reason to do this in SQL, not is this a good implementation of such a way to do so. Also @cnt is declared but never used.
-- Now for a question you didn't ask3) Should I be using SQL for this? NO==============================If you have to split a string into chunks like this in SQL then maybe you should think of a table valued function -- there are a lot of posts on string splitting functions on this site.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION