Skip to Content

Sublime Text 2 - Format SQL

I’ve posted before about formatting SQL in my IDE. I always inherit code where the previous developer formats their SQL different than I like. Currently I’m suffering from ALL CAPS SQL.

:::sql
SELECT INT_SUB_ID FROM INT_SUBCATEGORY WHERE INT_CAT_ID = 123

My eyes! My eyes!

I immediately hit Package Control in Sublime and sure enough there is a SQL package: Format SQL.

After installing (and restarting Sublime) you have a new option under Selection. Format > Format SQL Statement.

This got the indention correct but I was looking for some case changes:

:::sql
SELECT INT_SUB_ID 
FROM INT_SUBCATEGORY 
WHERE INT_CAT_ID = 123

Looking at the python-sqlparse library documentation there are some statements to change case for keywords and identifiers. So I cracked open \Sublime Text 2\Packages\Format SQL\FormatSQL.py and found this line:

:::python
return sqlparse.format(s, keyword_case="upper", reindent=True, indent_width=indent_size)

And modified it like so:

:::python
return sqlparse.format(s, keyword_case="upper", identifier_case="lower", reindent=True, indent_width=indent_size)

And now I get:

:::sql
SELECT int_sub_id 
FROM int_subcategory
WHERE int_cat_id = 123

Done!