Albert
Albert

Reputation: 317

How do I pretty format my SQL query in DBeaver?

I use DBeaver for my main IDE to query multiple databases. When I query a table, it automatically generates a long select statement with all columns in one line.

When I am making more complex queries, this gets very cumbersome to work with and read. I would like to view the select values on separate lines rather than one long string.

Is there an easy way (perhaps by using Notepad++ or Sublime Text) to break up the query (with line breaks after the commas)?

For example,

select name, address, city, state, country, zip_code, birthday, membership_type 
from customers

to

select
name,
address,
city,
state,
country,
zip_code,
birthday,
membership_type
from customers

Upvotes: 24

Views: 99314

Answers (8)

Gozy4
Gozy4

Reputation: 484

You can also use Prettier SQL Formatter. To do so you need to install it using npm or yarn as explained in the README:

npm install sql-formatter

Then go into DBeaver preferences -> SQL Editor -> Formatting. Choose External formatter and then add the command to run the sql formatter:

"C:\Program Files\nodejs\npx.cmd" sql-formatter -l postgresql -c "path_to_config_file.conf"

The -l option speficies the SQL dialect (you can skip it). The optional -c option allows you to specify a config file to customize the way you want your queries to be formatted (again more info in Prettier SQL formatter README).

Where you need to replace "C:\Program Files\nodejs\npx.cmd" with the correct path to your npx.cmd file, and if needed replace the path to the prettier config file "path_to_config_file.conf".

enter image description here

Upvotes: 0

Yurii Verbytskyi
Yurii Verbytskyi

Reputation: 2052

DBeaver has a shortcut for this purpose. In SQL Editor: enter image description here

Update May 2024:

DBeaver as of at least version 24.0.3.202404211624 has moved the Format SQL menu item to the Edit menu.

enter image description here

Upvotes: 39

Guido
Guido

Reputation: 958

There is another external SQL for DBeaver. It can be found here: SQLinForm SQL Formatter and can be included as external Formatter like this External Formatter for DBeaver

Upvotes: 9

Muhammad Ali
Muhammad Ali

Reputation: 101

Write click on the screen, this menu will be pop-up, and follow the instructions shown in the attached image. enter image description here

Upvotes: 0

Nisarg Patil
Nisarg Patil

Reputation: 1639

Select a query to be formatted and Ctrl+shift+F does the work. I am using DBeaver 6.3.0

Upvotes: 12

ParagDineshGupta
ParagDineshGupta

Reputation: 224

use backtick ` instead of quote " or '

Upvotes: -5

jwpfox
jwpfox

Reputation: 5242

In SublimeText I assume you have already installed Package Control? If not do so as soon as you can, it is a vital tool.

From Package Control there are a number of options to do what you want to do. Here are some options

I would start with SqlBeautifier and then try others to find which one suits your taste best.

Upvotes: 3

NonProgrammer
NonProgrammer

Reputation: 1387

Yes there is. I use Notepad++ because it is free and I use it for everything! Anyways, Notepad++ has a Plugin called: Poor Man's T-SQL Formatter. It works with a click of a button. You also get a few custom options like if you want to auto capitalize table names, etc.

enter image description here

Upvotes: 13

Related Questions