Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL string formatter [closed]

Does anyone know of a program, a utility, or some programmatic library, preferably for Linux, that takes an unformatted SQL string and pretty prints it?

For example I would like the following

select * from users where name = 'Paul' 

be changed to something like this

select *  from users where    name = 'Paul' 

The exact formatting is not important. I just need something to take a large SQL string and break it up into something more readable.

like image 294
Paul D. Eden Avatar asked Dec 24 '08 18:12

Paul D. Eden


People also ask

What is %d SQL?

For example, %d will format/convert the string to a numeric/integer string.

How do I unformat SQL query in SQL Developer?

Unformat SQLCopy and paste the SQL, and hit Ctrl K, Y to format your code to your own style. Make your changes, then once you're done, highlight the code, hit Ctrl and select Unformat.

How do I auto format a SQL query in SQL Server?

Format SQL Code in SSMS Query Window To use the SSMS add-in component we will have to close and then open SSMS after the install of the add-in. Then open a SSMS query window, highlight the code that needs to be formatted. Then select Format T-SQL Code from the SSMS Tools menu. Here is the code after the formatting.


2 Answers

Try sqlparse module's sqlformat

Check out sqlparse. It is a Python module that installs the command sqlformat. Usage is simple, e.g.:

sqlformat --reindent --keywords upper --identifiers lower my_file.sql 

I tried the aforementioned CLI alternatives, but:

  • sqlinform is out, because I want an open source CLI application.
  • fsqlf has only few features (create view is missing for example).

Thanks to sqlformat I even learned that "REF" is a reserved keyword in SQL:2011 and SQL:2008.

like image 100
felixhummel Avatar answered Sep 22 '22 18:09

felixhummel


Try fsqlf

fsqlf (http://sourceforge.net/projects/fsqlf/) is a command line or GUI program, open source, to format SQL. It supports having a formatting.conf file which allows you a lot of flexibility in how the final product looks.

Example 1:

☺  [wwalker@speedy:~]  $ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf   SELECT   f1 , f2 , fname , lName FROM tblName WHERE f1=true AND fname is not null ORDER BY lName asc 

Example 2:

☺  [wwalker@speedy:~]  $ vim formatting.conf # 2 character change  ☺  [wwalker@speedy:~]  $ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf   SELECT  f1 ,  f2 ,  fname ,  lName FROM tblName WHERE f1=true AND fname is not null ORDER BY lName asc 

Example 3:

☺  [wwalker@speedy:~]  $ vim formatting.conf # 1 character change  ☺  [wwalker@speedy:~]  $ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf   SELECT  f1 , f2 , fname , lName FROM tblName WHERE f1=true AND fname is not null ORDER BY lName asc 
like image 33
Wayne Walker Avatar answered Sep 22 '22 18:09

Wayne Walker