|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, July 13, 2012 9:19 AM
Points: 140,
Visits: 316
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 3:32 AM
Points: 1,
Visits: 233
|
|
It's not good. It doesn't take care if comments are in string (between single quote) when they aren't actually comments.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, February 14, 2011 9:14 PM
Points: 1,
Visits: 2
|
|
| This doesnt work for me. My version of Sybase only supports two arguments for charindex.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Sunday, November 04, 2012 11:17 PM
Points: 4,
Visits: 14
|
|
/*
No Limitations
Please refer mentioned below function to remove comments in String.
*/
CREATE FUNCTION [dbo].[Ufn_Remove_Comments] (@definition VARCHAR(MAX)) RETURNS @TblDefinition TABLE ( Definition VARCHAR(MAX) ) AS BEGIN
/* Author : Rajesha Soratoor Purpose : Returns text with out any comments in it.
Description : This code handle nested comments like Mentioned below. /* text /* text /* text */ */ text */ --Rajesha soratoor --soratoor */ DECLARE @startPosition INT = 0 DECLARE @endPosition INT = 0 DECLARE @PrevEndPosition INT = 0 DECLARE @comment VARCHAR(MAX) = '' DECLARE @len INT = 0 DECLARE @vbCrLf CHAR(2) SET @vbCrLf = CHAR(13) + CHAR(10)
/*Dealing with /* ... */ kind of comments */ WHILE Patindex('%/*%',@definition) <> 0 BEGIN SET @startPosition = Patindex('%/*%',@definition) SET @endPosition = Patindex('%*/%',@definition) SET @len = (@endPosition + 2) - @startPosition
SELECT @definition = STUFF(@definition, @startPosition, @endPosition - @startPosition + 2, --2 is the length of the search term '') SET @PrevEndPosition = @startPosition SET @startPosition = Patindex('%/*%',@definition) SET @endPosition = Patindex('%*/%',@definition) WHILE @startPosition > @endPosition OR @startPosition = 0 BEGIN SELECT @definition = STUFF(@definition, @PrevEndPosition, @endPosition - @PrevEndPosition + 2, --2 is the length of the search term '') SET @startPosition = Patindex('%/*%',@definition) SET @endPosition = Patindex('%*/%',@definition)
IF @startPosition = 0 and @endPosition = 0 BREAK END END
/*Dealing with --... kind of comments */ SELECT @startPosition = CHARINDEX('--',@definition) WHILE @startPosition > 0 AND CHARINDEX(@vbCrLf,@definition,@startPosition) > @startPosition SELECT @definition = STUFF(@definition, @startPosition, CHARINDEX(@vbCrLf,@definition,@startPosition) - @startPosition + 2, '' ) INSERT INTO @TblDefinition SELECT @definition RETURN END GO
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, July 13, 2012 9:19 AM
Points: 140,
Visits: 316
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, July 13, 2012 9:19 AM
Points: 140,
Visits: 316
|
|
You can handle all the nested comments by using the following code: DECLARE @comment VARCHAR(100), @endPosition INT, @startPosition INT, @commentLen INT, @substrlen INT, @len INT
WHILE (CHARINDEX('/*',@def)<>0) BEGIN SET @endPosition = charindex('*/',@def) SET @substrlen=len(substring(@def,1,@endPosition-1)) SET @startPosition = @substrlen - charINDEX('*/',reverse(substring(@def,1,@endPosition-1)))+1 SET @commentLen = @endPosition - @startPosition SET @comment = substring(@def,@startPosition-1,@commentLen+3 ) SET @def = REPLACE(@def,@comment,CHAR(13)) END
Kindest Regards, Shivaram Challa (http://challa.net - Home of the "Excel to CSV converter & Browsepad applications".) Forum Etiquette: How to post data/code on a forum to get the best help
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 1:44 AM
Points: 953,
Visits: 1,875
|
|
| I'm curious as to what circumstances you would plan on using this script - the hard part is usually getting developers to include comments in the first place; I can't think of any obvious justification for getting rid of them.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 7:54 AM
Points: 34,
Visits: 151
|
|
Andrew Watson-478275 (7/4/2012) I'm curious as to what circumstances you would plan on using this script - the hard part is usually getting developers to include comments in the first place; I can't think of any obvious justification for getting rid of them.
Just what I though! I comment nearly everything as it helps yourself as much as others.
Now if there was a script to ADD comments into SQL... Well that'd get 6 out of 5 stars!
~ UKGav
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 11:13 AM
Points: 46,
Visits: 204
|
|
| We used to do this when the code was placed in an off-site computer. It makes it more difficult for personal who aren't supposed to be in it to mess it up. I haven't done this with SQL, but I have done it with assembler, C, C++ and FORTRAN.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, July 13, 2012 9:19 AM
Points: 140,
Visits: 316
|
|
|
|
|