How can I remove empty lines in SQL Server Management Studio (SSMS)?
Asked Answered
O

9

14

I have many .sql files with lots of empty lines, for example,

WITH

  cteTotalSales (SalesPersonID, NetSales)

  AS

  (

    SELECT SalesPersonID, ROUND(SUM(SubTotal), 2)

    FROM Sales.SalesOrderHeader

    WHERE SalesPersonID IS NOT NULL

    GROUP BY SalesPersonID

  )

SELECT

  sp.FirstName + ' ' + sp.LastName AS FullName,

  sp.City + ', ' + StateProvinceName AS Location,

  ts.NetSales

FROM Sales.vSalesPerson AS sp

  INNER JOIN cteTotalSales AS ts

    ON sp.BusinessEntityID = ts.SalesPersonID

ORDER BY ts.NetSales DESC

Is there a way to remove these empty lines in SQL Server Management Studio (SSMS)?

This is what I would like to have:

WITH
  cteTotalSales (SalesPersonID, NetSales)
  AS
  (
    SELECT SalesPersonID, ROUND(SUM(SubTotal), 2)
    FROM Sales.SalesOrderHeader
    WHERE SalesPersonID IS NOT NULL
    GROUP BY SalesPersonID
  )
SELECT
  sp.FirstName + ' ' + sp.LastName AS FullName,
  sp.City + ', ' + StateProvinceName AS Location,
  ts.NetSales
FROM Sales.vSalesPerson AS sp
  INNER JOIN cteTotalSales AS ts
    ON sp.BusinessEntityID = ts.SalesPersonID
ORDER BY ts.NetSales DESC
Ordway answered 30/4, 2010 at 5:51 Comment(0)
C
3

It is not built in. The find and replace can be used with regex's and someone crafty may have a solution for that.

Caines answered 30/4, 2010 at 5:57 Comment(2)
Thanks, I worked out this by myself like this: Replace using regular expression, and find line breaks at the beginnign of line: ^\n. Repalce with nothing.Ordway
@all, check the next answer for the stepsFarrier
C
30

You can do it using the regular expression in SSMS:

  1. Ctrl-H to bring up the Find And Replace window
  2. Select USE -> Regular Expressions
  3. Put ^\n in the Find What
  4. Keep Replace With empty
  5. Click Replace (All)

Good luck

Cable answered 25/8, 2010 at 21:5 Comment(3)
This should be the answer, not sure why the other one is.Instructor
@NoSaidTheCompiler because it is the correct answer, was given 6 minutes after the question was asked and allowed the asker to find the specific solution needed. As opposed to this one which propose the same solution, copies information that was already in a comment by the asker on the accepted answer and was given 4 months after the answer was marked as accepted. It may be a better answer since it is clearer but we can see this is reflected in the votes. All is as it should be, the system works! :-)Fennie
@Fennie - totally makes sense. Thanks for taking time to provide the explanation.Instructor
L
21

I have gone through the below steps, and it worked for me.

  1. Menu ToolsCustomizeCommandsAdd commandEditDelete blank lineOK.
  2. The Delete Blank Lines menu appears in beside the File menu.
  3. Open/Select Query - Click "Delete Blank Lines".
  4. Enjoy.
Loveinidleness answered 6/2, 2019 at 8:13 Comment(3)
Thanks - this was the only thing that worked for me Regex couldnt find \^nTriumphant
Strange, I also had to resort to this as RegEx didn't work. Would be interesting if someone could shed some light on why.Perorate
This is the correct answer - why muck about with RegEx when there is a built in way to do it?Slaby
N
6

You can use: Find and replace

  • Find what: \n\n
  • Replace with: \n
  • use: 'regular expressions'

it is described here:

http://my.safaribooksonline.com/book/databases/microsoft-sql-server/9781617290473/using-regular-expressions-in-ssms/ch21lev1sec1#X2ludGVybmFsX0h0bWxWaWV3P3htbGlkPTk3ODE2MTcyOTA0NzMlMkZjaDIxbGV2MXNlYzImcXVlcnk9

Niddering answered 2/7, 2013 at 13:48 Comment(1)
It seems to work here Find what: \n\n* and Replace with: \nCapper
M
4

Menu ToolsCustomizeCommandsAdd commandEditDelete blank lineOK.

Mounts answered 5/1, 2019 at 12:58 Comment(0)
C
3

It is not built in. The find and replace can be used with regex's and someone crafty may have a solution for that.

Caines answered 30/4, 2010 at 5:57 Comment(2)
Thanks, I worked out this by myself like this: Replace using regular expression, and find line breaks at the beginnign of line: ^\n. Repalce with nothing.Ordway
@all, check the next answer for the stepsFarrier
A
0

Redgate Sql Toolbelt is good for this. This package has Sql Prompt and Sql Refactor which allows easy formatting of your query (even from very very bad formatting). It will allow you to cut on spaces, move stuff around according to your needs.

Code completion As you type SQL Prompt provides unobtrusive support, suggesting appropriate keywords, tables, views, and other database objects. It even suggests complete join conditions based on foreign key constrains or matching column names. Where it makes sense SQL Prompt will complete entire statements for you, such as INSERT or ALTER VIEW.

SQL reformatting (Pro edition only) The Format SQL command reformats any SQL to match your chosen coding style. Clear and accurate formatting make it much easier to understand complex SQL, and helps maintain a consistent style across your entire team.

It's not free but definitely worth a try if you have budget for it.

Anachronistic answered 25/8, 2010 at 21:36 Comment(1)
Is there a corresponding "coding standard" for SQL?Bjork
H
0

Those of you finding that a regex find-and-replace of ^\n with blank doesn't work, I had the same problem because my file contained CR LF rather than just LF. In this case using ^\r\n instead worked for me.

Handsomely answered 20/10, 2022 at 15:29 Comment(0)
A
0

For all those (like me) who have issues with ^\n not finding any regex matches, I was able to remove the empty lines in my SSMS editor using;

\r\s+          in the Find box

\r             in the Replace box
Afc answered 25/1 at 14:33 Comment(0)
D
-1

Use Find and Replace with fine '^\n' and in replace leave it blank.

Additionally, check Use regular expression in the Find option.

Deegan answered 17/3, 2015 at 11:45 Comment(1)
What do you mean by "with fine"? (Please respond by editing your answer (without "Edit:", "Update:", or similar), not here in comments).Bjork

© 2022 - 2024 — McMap. All rights reserved.