Format multi-line sql queries to single line

 

Format multi-line sql queries to single line queries

I had a requirement to change multi-line sql queries to single line queries.

UPDATE table_name

SET

    column1=value

WHERE

    condition=value1

    AND condition2=value1;

UPDATE table_name

SET

    column1=value2

WHERE

    condition=value2

    AND condition2=value2;

to

UPDATE table_name SET column1=value WHERE condition=value1 AND condition2=value2;

UPDATE table_name SET column1=value2 WHERE condition=value2 AND condition2=value2;


I have tried different sql formatter in VS Code and Notepad++, all formatting into multiple lines.


Then I came to below stackoverflow link and achieved single line format in in two steps.

Step 1: replace all (?:\h*\R\h*)+ with space

Step 2: replace all with ;\r\n


References

https://stackoverflow.com/questions/52003347/notepad-turn-big-query-from-sql-server-into-one-line