Search code examples
notepad++string-formatting

Notepad++ - Turn big query from SQL Server into one line


I've a big query from SQL Server in notepad++ (1012 lines) and I want to pass this query to only one line. For example, I've this:

SELECT *
FROM tableA
WHERE Field_A = 1

And I want to pass to this:

SELECT * FROM tableA WHERE Field_A = 1

I'm trying with the following codeS:

  1. Open the Replace dialog ( Ctrl + H )
  2. Check the Wrap around option
  3. Choose the Regular expression search mode
  4. Fill in the regex (\h*\R)+ in the Find what: zone
  5. Fill in the regex \x20 in the Replace with: zone
  6. Click on the Replace All button

But it's creating me some more new lines with a big space between the code.

How can I do this?


Solution

  • You have to remove the spaces just after the linebreak, use this regex (?:\h*\R\h*)+

    • Ctrl+H
    • Find what: (?:\h*\R\h*)+
    • Replace with: A SINGLE SPACE
    • check Wrap around
    • check Regular expression
    • Replace all

    Explanation:

    (?:         : start non capture group
        \h*     : 0 or more horizontal spaces
        \R      : any kind of linebreak (ie. \r, \n, \r\n)
        \h*     : 0 or more horizontal spaces
    )+          : end group, repeated 1 or more times
    

    Replacement:

    A single space
    

    Given example like:

    SELECT *
    
                FROM tableA
    
    
    
    WHERE Field_A = 1
    

    Result for given example:

    SELECT * FROM tableA WHERE Field_A = 1