Live data from Hacker News

How about trailing commas in SQL?

peter.eisentraut.org

151–160 of 272 posts

Re: How about trailing commas in SQL?

#151
post #7

I feel like this ship has sailed. SQL has been around for more than 50 years and everyone who needs to generate it has already put that extra `if` statement in to suppress trailing commas. What annoys me far more often is the lack of support for trailing commas in JSON.

What extra "if"?

  ', '.join(fields)
;)

Re: How about trailing commas in SQL?

#153
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

Another trick is, if you're programmatically building a SQL statement - adding "WHERE 1=1" makes things easier ... like so:

  SELECT *
  FROM table
  WHERE 1=1
That way, if you want to filter down the result, everything programmatically appended just needs an "AND ..." at the start, like:

  SELECT *
  FROM table
  WHERE 1=1
  AND age > 21
  AND xyz = 'abc' ...
Because without "WHERE 1=1", you'd had to handle the first condition different than all subsequent conditions.

Re: How about trailing commas in SQL?

#154

Earlier quoted context omitted.

I often do this with boolean WHERE filters when I'm doing interactive exploration on some data: SELECT ... WHERE foo = 1 AND bar = 2 I want to comment out a line (i.e. "--foo = 1"), but would break the syntax. The solution is to start with "WHERE true": SELECT ... WHERE true --AND foo = 1 AND bar = 2 Now you can comment/comment anything. (Putting the AND at end of each line has the same problem, of course, and requir…

Huh, I've always used "WHERE 1 = 1 AND ...". Using `true` looks more clean.

Not all sql variants support "true". e.g. SQL Server doesn't.

Re: How about trailing commas in SQL?

#155
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

That's as bad as using regular expressions: now you have TWO problems. Why do you seem to think you've cleverly solved the problem, when you've just moved the problem somewhere else just as bad, by blithely messing with the standard formatting conventions universally used by most human written languages and programming languages in the world? Programming languages borrow commas from human written languages, and no hu…

Programming languages borrow commas from human written languages, and no human written languages have leading commas

How is this any different from leading periods, which seems to have become the standard across several of the most popular programming languages?

    myobject
        .somefunc()
        .otherfunc();
It's not subjectively pleasant in my opinion, but I think it's hard to argue that it doesn't improve maintainability and (apparently) readability for the masses.

Re: How about trailing commas in SQL?

#156
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

Another trick is, if you're programmatically building a SQL statement - adding "WHERE 1=1" makes things easier ... like so: SELECT * FROM table WHERE 1=1 That way, if you want to filter down the result, everything programmatically appended just needs an "AND ..." at the start, like: SELECT * FROM table WHERE 1=1 AND age > 21 AND xyz = 'abc' ... Because without "WHERE 1=1", you'd had to handle the first condition diff…

Similarly, you could select NULL as your leading column, and prepend commas by that means.

That method does impact the result set, and using it for CTAS or bulk insert would require more care in column selection.

Re: How about trailing commas in SQL?

#157
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

That's as bad as using regular expressions: now you have TWO problems. Why do you seem to think you've cleverly solved the problem, when you've just moved the problem somewhere else just as bad, by blithely messing with the standard formatting conventions universally used by most human written languages and programming languages in the world? Programming languages borrow commas from human written languages, and no hu…

I don't always read code(*), but when I do...

   o 
  ムワ
The padding and separators are the least issues I wish I only had there.

We don't need philosophy or morals (or jobs where that's important), we just need the way to edit these damn lines without anything screaming "syntax error!" or parasitic "++--" diffs every time you make a change. “When art critics get together they talk about Form and Structure and Meaning. When artists get together they talk about where you can buy cheap turpentine.”

-

(*) That classic claim didn't turn out true for me after so many years, that I suspect developers simply avoid admitting that it's not true for them either.

Re: How about trailing commas in SQL?

#158
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

Recently I've been formatting like this but with tabs so the first column is aligned with subsequent columns:

    SELECT
        a
    ,   b
    ,   c
    FROM ...

Re: How about trailing commas in SQL?

#159

Earlier quoted context omitted.

God, everyone's going to hate me for this. (I will have earned it, I think.) SELECT ... WHERE foo = 1 AND bar = 2 Each keyword gets a new line, the middle gutter between keyword and expressions stays in the same place, and things get really, really fugly if I need a subselect or whatever. Any given line can be commented out. (And no, none of that leading comma bullshit, somehow that looks nasty to me.) Downvote this…

You may have missed what I was getting at. In order to quickly comment out the "foo = 1" here, you cannot simply comment out the whole line because it would become syntactically invalid: SELECT ... --WHERE foo = 1 AND bar = 2 I have a single keyboard shortcut to comment/uncomment a line because I like to work briskly with as little unnecessary typing. It has nothing to do with indentation or being "fugly". I'm talkin…

I slowly come to creating vim scripts for all that. I already use `alt-,` for triggering the final comma on a line. Maybe with modern LLMs I just need to prompt a vimscript that detects where the line is (SQL condition, array item, json, etc) and use `alt-,` to do the right thing. Or something like "fixing" the whole block with `g,ap`. Because all this is irritating and no one does anything with it for decades.

Re: How about trailing commas in SQL?

#160
last time I was on a project that involved a lot of data analysis/writing SQL, I was gonna write a plugin for "sloppy SQL" which would let me use trailing commas and some other qol features, but that project ended before I got around to it.
Post reply on HN