Want to remove all trailng -- comments from my view definitions. I cannot get it to work. I get very different results depending on how the comment is formatted. Can someone please help me with the cte. I want to remove starting at -- up until the char(13) + char(10)
------comment
--- comment
CREATE VIEW [dbo].[v_test_columns] AS SELECT top 2 name AS [column name], --- comment object_name(object_id) as [table name], --comment2 max_length as [max length] FROM sys.columns declare @definition varchar(max) declare @select int declare @from int declare @columns varchar(max) SELECT @definition = m.definition FROM sys.sql_modules m WHERE m.object_id = OBJECT_ID('v_test_columns', 'V'); --DECLARE @definition2 varchar(max) = RTRIM(LTRIM(REPLACE(REPLACE(@Definition, CHAR(9), 'X'), CHAR(13) + CHAR(10), 'Y'))) --print @definition2 SET @select = CHARINDEX('select ', @definition); SET @from = CHARINDEX('from ', @definition); set @columns = SUBSTRING(@definition, @select + 7, @from - @select - 7); print @columns -- remove the first trailing dash comments SET @columns = Left(@columns, CHARINDEX('--', @columns ) -1) + Right(@columns, len(@columns) - CHARINDEX(CHAR(13) + CHAR(10), @columns ) + 1) --print @columns -- Now use a recursive cte to find each of the matches within the ReportDefinition report definition and give a preview ;with T(columns1, start, pos) as ( select @columns, cast(1 as int), charindex('--', @Columns) union all select @columns, cast(pos + 1 as int), charindex('--', @Columns, pos + 1) from t where pos > 0 ) SELECT @columns = Left(@columns, CHARINDEX('--', @columns, pos)) + Right(@columns, len(@columns) - CHARINDEX(CHAR(13) + CHAR(10), @columns, pos ) ) FROM T WHERE Pos > 0 print 'columns: ' + @columns
- Moved by Lydia ZhangMicrosoft contingent staff, Moderator 5 hours 35 minutes ago more appropriate