# Introducing sqlfmt, an auto-formatter for dbt SQL

**URL:** <https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687>\
**Category:** Show and Tell\
**Tags:** sqlfmt\
**Created:** [January 27, 2022, 3:18am UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687 "2022-01-27T03:18:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [January 27, 2022, 3:18am UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/1 "2022-01-27T03:18:29Z")

</div>

Since becoming an analyst and learning SQL, I have formatted my SQL queries in every possible way. After meeting the folks at dbt Labs (nee Fishtown) four years ago, I was inspired by their SQL style guide; after spending more time writing python and using black, I desperately wanted an auto formatter like black for dbt SQL.

Over the past few months, I have been hacking away at that tool, and today it works with dbt jinja and creates well-formatted, modern-looking SQL. It’s not a linter (use SQLFluff for that) – it just does one thing and does it well.

You can try it at [sqlfmt.com](http://sqlfmt.com/), or check out the [repo on Github](https://github.com/tconbeer/sqlfmt). Or if you like to live dangerously, `pipx install shandy-sqlfmt`

It’s still really early innings. There are more features and integrations I’d like to build (and I’m sure plenty of bugs yet to be found). I would really, really value any contribution from this community, whether that’s feedback, an issue, or a PR. Thanks for checking it out; please let me know what you think!

---

<div class="post-metadata">

**Author:** ![jhopkins](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jhopkins/32/890_2.png) [@jhopkins](https://discourse.getdbt.com/u/jhopkins)\
**Post date:** [January 31, 2022, 2:09pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/2 "2022-01-31T14:09:17Z")

</div>

Installed and started playing around with it over the weekend. Great work.

I love an opinionated formatter: we’ve been using `black` for a while in our Python libraries, and it really reduces the cognitive (and conversational) load. So I’m excited for an equivalent for our SQL files. (And I like that it starts from the dbt Labs style guide.)

---

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [January 31, 2022, 3:47pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/3 "2022-01-31T15:47:15Z")

</div>

Thanks for contributing!

---

<div class="post-metadata">

**Author:** ![janmeppe](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janmeppe/32/1059_2.png) [@janmeppe](https://discourse.getdbt.com/u/janmeppe)\
**Post date:** [May 12, 2022, 11:40am UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/4 "2022-05-12T11:40:07Z")

</div>

Hi,

Is it possible to force the `and`s to align here?

Autoformatted:

```sql
select *
from historic_answers ha
where
    subject = 'Math' and learningobjective = "Addition" and dt_utc > date(
        '2020-10-01'
    ) and testschool = false

```

Desired:

```sql
select *
from historic_answers ha
where 1=1
    and subject = 'Math' 
    and learningobjective = "Addition" 
    and dt_utc > date('2020-10-01') 
    and testschool = false

```

Or does this go against some of the fundamental rules of your formatter?

---

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [May 23, 2022, 6:28pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/5 "2022-05-23T18:28:40Z")

</div>

I agree your solution is better. It’s been on my mind to improve the formatting of chained boolean operators with parentheses for some time. Can you [open an issue](https://github.com/tconbeer/sqlfmt/issues/new/choose) for this?

---

<div class="post-metadata">

**Author:** ![janmeppe](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/janmeppe/32/1059_2.png) [@janmeppe](https://discourse.getdbt.com/u/janmeppe)\
**Post date:** [May 24, 2022, 7:05am UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/6 "2022-05-24T07:05:49Z")

</div>

Good that you mention it, should I open two issues:

1. Force separate `where` clauses on single lines (even if they don’t exceed max width)
2. Improve formatting of chained boolean operators with parentheses

Or do you see these two issues as one?

P.S. it would be awesome if there was a way to change the line length in the interactive web version 🙂 I use it a lot when showcasing it to people but the default line length is a bit short for most people

---

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [May 31, 2022, 5:27pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/7 "2022-05-31T17:27:58Z")

</div>

Sorry, my notifications for discourse started going to Spam! #2 is what I had in mind, it basically implies #1… the real issue today is the interaction between the parentheses-driven line splitting and merging and the boolean operator-driven line splitting and merging.

Re: line-length online, sure thing! I’ll create an issue for that one on the [sqlfmt.com](http://sqlfmt.com) repo

---

<div class="post-metadata">

**Author:** ![luke\_bassett](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@luke\_bassett](https://discourse.getdbt.com/u/luke_bassett)\
**Post date:** [June 2, 2022, 6:57pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/8 "2022-06-02T18:57:38Z")

</div>

Just wanted to say thanks for making this! I’m using it on on a project at work and so far it’s been great!

---

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [June 3, 2022, 7:58pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/9 "2022-06-03T19:58:15Z")

</div>

@janmeppe Line length config is now available on [sqlfmt.com](http://sqlfmt.com): click “Configure Formatting” next to the blue button for the available options.

---

<div class="post-metadata">

**Author:** ![obar1](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/obar1/32/1221_2.png) [@obar1](https://discourse.getdbt.com/u/obar1)\
**Post date:** [October 13, 2022, 4:27pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/10 "2022-10-13T16:27:16Z")

</div>

No way you can hack this or sqlfluff into dbt cloud… any option to have decent formattation on dbt cloud as we speak or coming soon?

---

<div class="post-metadata">

**Author:** ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)\
**Post date:** [October 13, 2022, 5:38pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/11 "2022-10-13T17:38:21Z")

</div>

> [@obar1](#):
>
> hack this or sqlfluff into dbt cloud

Not at the moment, but hopefully one day 🤞

---

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [October 20, 2022, 6:09pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/12 "2022-10-20T18:09:50Z")

</div>

The new dbt Cloud IDE has a format button that is powered by sqlfmt! It’s in Beta right now, GA early next year

---

<div class="post-metadata">

**Author:** ![obar1](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/obar1/32/1221_2.png) [@obar1](https://discourse.getdbt.com/u/obar1)\
**Post date:** [October 21, 2022, 5:17am UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/13 "2022-10-21T05:17:39Z")

</div>

great feature - I hope it can be customised to match your/my formatting preference

---

<div class="post-metadata">

**Author:** ![johnny.huang](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@johnny.huang](https://discourse.getdbt.com/u/johnny.huang)\
**Post date:** [February 26, 2023, 10:00am UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/14 "2023-02-26T10:00:44Z")

</div>

hey on the [http://sqlfmt.com/](http://sqlfmt.com/) website you are able to configure the max line length. Is this config (and other settings) possible in dbt?

---

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [February 26, 2023, 8:13pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/15 "2023-02-26T20:13:33Z")

</div>

No, formatting in dbt Cloud is currently not configurable. You can configure line length if you download the sqlfmt CLI and use it with dbt Core.

See: [Installation | sqlfmt](https://docs.sqlfmt.com/getting-started/installation)  
And: [Configuring sqlfmt | sqlfmt](https://docs.sqlfmt.com/getting-started/configuring-sqlfmt)

---

<div class="post-metadata">

**Author:** ![johnny.huang](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@johnny.huang](https://discourse.getdbt.com/u/johnny.huang)\
**Post date:** [February 27, 2023, 11:26am UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/16 "2023-02-27T11:26:26Z")

</div>

shame… I hope this comes in future releases of dbt Cloud

---

<div class="post-metadata">

**Author:** ![manon\_bijenkorf](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@manon\_bijenkorf](https://discourse.getdbt.com/u/manon_bijenkorf)\
**Post date:** [April 11, 2023, 12:37pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/17 "2023-04-11T12:37:57Z")

</div>

I tried this over some of our SQL code but I see that it reorders the columns in my query, is this intended behaviour?

---

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [April 11, 2023, 4:50pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/18 "2023-04-11T16:50:37Z")

</div>

No, it shouldn’t reorder columns. Can you please share an example either here or by opening an issue on GitHub?

---

<div class="post-metadata">

**Author:** ![manon\_bijenkorf](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@manon\_bijenkorf](https://discourse.getdbt.com/u/manon_bijenkorf)\
**Post date:** [April 11, 2023, 5:51pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/19 "2023-04-11T17:51:23Z")

</div>

I guess it is expected? If you look at rule “Rule\_ST06” in SQLfluff, that is exactly what is happening with sqlfmt as well… I assume those rules are also used by sqlfmt? Or how does sqlfmt use SQLfluff?

---

<div class="post-metadata">

**Author:** ![ted.conbeer](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ted.conbeer/32/42_2.png) [@ted.conbeer](https://discourse.getdbt.com/u/ted.conbeer)\
**Post date:** [April 11, 2023, 7:18pm UTC](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687/20 "2023-04-11T19:18:33Z")

</div>

sqlfmt and sqlfluff are two completely different projects. This post is about [sqlfmt.com](http://sqlfmt.com), which is the pypi package `shandy-sqlfmt`. Unfortunately the PyPI package called `sqlfmt` is a separate, abandoned project that uses sqlfluff under the hood. Is it possible you installed `sqlfmt` instead of `shandy-sqlfmt`?

[Next page](https://discourse.getdbt.com/t/introducing-sqlfmt-an-auto-formatter-for-dbt-sql/3687.md?page=2)
