Notepad++ snippets

How to remove comments in SQL with Notepad++

How to remove single line comments

Let say you have the following query:

Notepad++ replace 1


/*
This is a multi-line comment to help understand the not difficult query
*/

-- Difficult query 1
SELECT *
FROM MyTable
WHERE Condition=1


-- Difficult query 2
SELECT *
FROM MyTable
WHERE Condition=2

/*
This is another multi-line comment to help understand the not difficult query
*/

and you want remove all the inline comments:

Notepad++ replace 3


/*
This is a multi*/

SELECT *
FROM MyTable
WHERE Condition=1


SELECT *
FROM MyTable
WHERE Condition=2

/*
This is another multi*/
                            

To do this in Notepad++, you can just hit CTRL + H for Replace window and the select the following:

Notepad++ replace 2

  • Find what: [--]+.*\r\n
  • Replace with: leave empty
  • Flag Regular expression
  • Do not flag . matches new lines

How to remove multiline comments

Let say you have the following query:

Notepad++ replace 1


/*
This is a multi-line comment to help understand the not difficult query
*/

-- Difficult query 1
SELECT *
FROM MyTable
WHERE Condition=1


-- Difficult query 2
SELECT *
FROM MyTable
WHERE Condition=2

/*
This is another multi-line comment to help understand the not difficult query
*/
                            

and you want remove all the multiline comments:

Notepad++ replace 3


-- Difficult query 1
SELECT *
FROM MyTable
WHERE Condition=1


-- Difficult query 2
SELECT *
FROM MyTable
WHERE Condition=2
                            

To do this in Notepad++, you can just hit CTRL + H for Replace window and the select the following:

Notepad++ replace 2

  • Find what: /\*.*?\*/n
  • Replace with: leave empty
  • Flag Regular expression
  • Do flag . matches new lines

Back to Notepad++ cookbook page