45

As one might expect, Stack Overflow has a lot of questions regarding databases!

It would be a massive benefit for those prepared to answer the question if it were possible for the OP to enter CSV data and have it formatted into a table. Those who answer the question should have the option of extracting that data back as CSV (or better still as a "create table and populate with data SQL" statement).

For example, the OP would enter:

Id,Name,Age
1,James,20
2,Jane,20

And this data would appear like this in the question:

+--+-----+---+
|Id|Name |Age| [GetCSV] [GetCreateTable]
+--+-----+---+
|1 |James|20 |
|2 |Jane |20 |
+--+-----+---+

where [GetCsv] and [GetCreateTable] are buttons that would allow access to the original CSV and a relevant create table statement respectively.

The [GetCreateTable] button would provide something along the lines of

CREATE TABLE SOTemp
(
    Id int,
    Name varchar(255),
    Age int
);
INSERT INTO SOTemp VALUES (1,"James",20);
INSERT INTO SOTemp VALUES (2,"Jane",20);

I realise that column types would need to be inferred (perhaps you could have an option to set them, or maybe the OP could post their CREATE TABLE statement and Stack Overflow can infer the table from that), but this would make it dramatically easier and quicker for the people who are answering questions.

17
  • What if someone actually wants to display some CSV data?
    – Cerbrus
    Commented Jul 10, 2015 at 11:15
  • 1
    Tough! I'm joking ... we already use 4 spaces to 'escape' code blocks ... I'm sure there must be a spare delimiter knocking around in the SO delimiter repository! Even enclosing the csv in a block like [SOCsvToTable] csv data [\SOCsvToTable] would work. Commented Jul 10, 2015 at 11:17
  • Okay, say we use a single pipeline (|), like you'd use 4 spaces for a normal code block, what's to stop users from using the wrong formatting option? Users have enough trouble formatting code as it is, already. Those [] blocks you suggested seem more reliable, indeed.
    – Cerbrus
    Commented Jul 10, 2015 at 11:20
  • 1
    I agree some new users are useless at formatting code (been there myself a few weeks ago!), but then why should the rest of us suffer because of their unfamiliarity/incompetence :0) Commented Jul 10, 2015 at 11:22
  • "Suffer" is a little exaggerated, don't you agree? I'm not saying the suggestion is a bad idea, just that it's not as simple as claiming some character as SQL block formatting indicator.
    – Cerbrus
    Commented Jul 10, 2015 at 11:24
  • Lol. Suffer may be too strong, agreed, but I am talking about me making other people suffer with my formatting too! I'm no better than anyone else. Worse than most I expect! Commented Jul 10, 2015 at 11:26
  • This all stems from the fact that I would like to improve my DB skills ... solving other people's problems is an ideal way to do this, but i haven't got time to go creating temporary tables every time i want to have a crack at answering a question. If i was better at DB stuff then maybe I would though, hey?! Commented Jul 10, 2015 at 11:30
  • 1
    I'm pretty sure that there are already tools on the web which you can use to format a CSV input into a similar representation. This shouldn't be the responsibility of SE to implement; it's better to keep the SE editor simple and lightweight IMO.
    – l4mpi
    Commented Jul 10, 2015 at 12:44
  • 1
    Found one on google: convertcsv.com/csv-to-flat-file.htm This seems to have all the options you'll ever need.
    – l4mpi
    Commented Jul 10, 2015 at 12:56
  • And another one sensefulsolutions.com/2010/10/format-text-as-table.html Commented Jul 10, 2015 at 13:18
  • 2
    @l4mpi Why have Stack Snippets when JSFiddle exists? The answer is that it is such a useful feature for helping people solve problems quickly that it just makes sense. And your suggestion means that an expert has to go moving everything to an external tool to properly see the data. Wouldn't it be really efficient if this were built in to SE? Commented Jul 10, 2015 at 15:36
  • 1
    How would the 'create table' statement be determined? I'm dubious that it would get the types right for all DBMS (any DBMS?). The VARCHAR(255) looks like an exaggeration for the sample data shown, for example. And detecting the correct temporal types for temporal data will be … challenging? IMO, the schema (an outline schema) should be posted with the question anyway. It would be nice to ensure there was always a table name — it is remarkable how frequently people ask about nameless tables! I've not run into any major problems when data has been presented; I've often had to create data. Commented Jul 10, 2015 at 20:36
  • 5
    SQL Fiddle has an amazing TEXT to DLL feature. It does a pretty good job of inferring types for most popular databases.
    – PM 77-1
    Commented Jul 10, 2015 at 21:08
  • 2
    Providing well formatted example data and table structure is minimal effort. No matter how much easier you make it - Sadly most OPs don't even care doing that.
    – juergen d
    Commented Jul 12, 2015 at 12:25
  • 1
    Just a quick suggestion, instead of permanent table - declare @SOTemp table ( ... ). As no one would want to persist OP's table/data in question. Commented Sep 10, 2015 at 14:51

2 Answers 2

7

It would be a massive benefit for those prepared to answer the question if it were possible for the OP to enter csv data and have it formatted into a table. Those who answer the question should have the option of extracting that data back as csv (or better still as a "create table and populate with data sql" statement).

The CREATE TABLE and INSERT statements should be provided by OP and not the responsibility of the community to prepare a test case on behalf of OP. Once I posted a discussion about A pointer to “how to post a test case with any Oracle SQL or PL/SQL question”. There were mixed opinions about keeping it in the wiki, while this is an endemic problem throughout Stack Overflow.

There are few things which annoys me when OP just posts the data in tabular format:

  • Either I have to reverse engineer and prepare the create and insert statements.
  • Or, use 3rd party tools like SQL Fiddle to generate DDL from Text.

Now all this could be simply avoided if OP provides the create and insert statements. Not to forget the annoying double-quotation marks and no control over choosing the data types. What about DATE values? Will the system handle the NLS dependency?

I like this article about How to ask questions by Thomas Kyte.

The people reading the questions are not compilers!

In my opinion, it is solely OP's responsibility to provide the create and insert statements.

4

I vote against implementing this as a feature, because way too many people are struggling with the current formatting options already.
Of course, such a formatting feature could be made available based on reputation or something, but people don't seem to like that idea.

But I too get annoyed at database questions, sometimes because I wish I has a graphical view of their DB, and the OP only provided an SQL dump, and sometimes because the OP actually made the effort of creating some ASCII art, but I wish I had an actual SQL to play around with.

On Code Golf SE it is common practice to use stack snippets to display leaderboards, i.e. tables.
So why not use them to process CSV?
Admittedly, this is more of a workaround than an actual solution, but I think it would still be better than having a ton of 1-rep users presented with yet another formatting feature just waiting to be abused over and over.
Users who do a little research and read through the help center can be trusted with formatting options though, IMO.

So, my suggestion is:
Come up with some JavaScript to take CSV, and format it, or build SQL from it (or whatever your heart desires), and put that somewhere into the help center.

Example snippet & data:
(Warning: JS was made in <3 min and does not reliably parse CSV, it's only for PoC!)

var d=document,b=d.body,h='',s=d.createElement('style');b.firstChild.textContent.trim().split('\n').forEach(function(l){h+='<tr>';l.split(',').forEach(function(c){h+='<td>'+c+'</td>'});h+='</tr>'});b.innerHTML='<table>'+h+'</table>';s.innerHTML='td{border:solid 1px #CCC;padding:5px;}';d.head.appendChild(s);
Id,Name,Age
1,James,20
2,Jane,20

The JS can be golfed to a one-liner, the data could be put in the HTML or CSS field (I chose HTML here), so by default, you could look at the raw CSV data, and upon pressing "Run code snippet", you'd have it displayed as a nice table.
Of course the code could (and likely should) then be expanded to switch between table and SQL view, display the table header in <th>s instead of <td>s, etc.

An entry could then be added to the Editing Help page (I know it's not technically part of the help center, but still...), like:

Database schemas, CSV and SQL

In order to present your database schema in the most useful and clear way, the community has come up with some code. To use it, press Ctrl+M or click on "JavaScript/HTML/CSS snippet" in the editor, then insert your SQL/CSV into the "HTML" field and paste this line into the "JavaScript" field:

var d=document,b=d.body,h='',s=d.createElement('style');b.firstChild.textContent.trim().split('\n').forEach(function(l){h+='<tr>';l.split(',').forEach(function(c){h+='<td>'+c+'</td>'});h+='</tr>'});b.innerHTML='<table>'+h+'</table>';s.innerHTML='td{border:solid 1px #CCC;padding:5px;}';d.head.appendChild(s);

Edit:

Someone posted a comment (which was soon after deleted) with a link to an answer on Meta DBA with the same idea.
Given that the status of the question is (even for DBA!), I think this is by far our best option.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .