James Wang Blog

Tidy sql posted on your blog

As a sql developer, DBA.  we often post sql code on our blog.    Take this sql for example,

select pub_name, count(qty) as orders, sum(qty) as total 
from sales inner join titles on (sales.title_id=titles.title_id)
right join publishers on (publishers.pub_id=titles.pub_id)
group by pub_name

It's really difficult for others to read. and how about this one after beautify previous sql:


SELECT   pub_name,
         Count(qty) AS orders,
         Sum(qty)   AS total
FROM     sales
         INNER JOIN titles
           ON (sales.title_id = titles.title_id)
         RIGHT JOIN publishers
           ON (publishers.pub_id = titles.pub_id)
GROUP BY pub_name

Yes, it's really good for your readers.

Now, you can do this instantly with the help of a free online sql formatter:

http://www.dpriver.com/pp/sqlformat.htm


1. Paste original sql code into input sql text box, then select corresponding database type, and output type(sql(html:span) should be ok).

2. Click "format sql" button

3. Copy Html code from text box at the bottom of that page.

4. In your FCKeditor(If you're using SUBTEXT which is used by sqlteam weblogs), switch to source code mode by click "source code icon" in the up-left corner,
then paste html code, after that switch back to normal mode by click that button again.

5. You are done.

I think it should as easy as this to tidy sql in other blog systems like wordpress, community server. Anyway, I will check those blogs system, and let you know the result in next following days.





Legacy Comments


Brian Tkatch
2009-07-29
re: Tidy sql posted on your blog
That's nice. Thanx for the link.

One thing i would want added to the options, is to put a blank line after keywords. That is, instead of:

SELECT COl1,
Col2
FROM tab1
tab2

I want:

SELECT
COl1,
Col2
FROM
tab1
tab2