logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 Make ugly SQL pretty

Posted on
10/17/2003
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
All
Product:
.NET Framework



Download... the code. (45 kb)

Have you ever gotten a SQL statement from someone that is, to put it mildly, difficult to decipher. Well, I have and was getting kind of tired of trying to make sense of the jumbled code. Given, the need is the mother of me getting off my butt, I wrote a little utility. It was actually so simple, I was actually surprised. Code is below. Note that the code uses the Microsoft.VisualBasic reference (yeah, I know, but it has the really useful case insensitive Replace function)

The before and after shot.

Add the following to a Class

private string MakeSqlPurty(string JumbledSql) 
{

string sPurtySql;

sPurtySql = JumbledSql;

//find and replace all the "," with carriage returns & commas
sPurtySql = sPurtySql.Replace(", ", ",");
sPurtySql = sPurtySql.Replace(",", ",\n\t");

//move FROM, WHERE, ORDER BY, GROUP BY down by 2 lines
sPurtySql = Strings.Replace (sPurtySql, " FROM ", "\n\nFROM ", 1, -1, CompareMethod.Text);
sPurtySql = Strings.Replace (sPurtySql, " WHERE ", "\n\nWHERE ", 1, -1, CompareMethod.Text);
sPurtySql = Strings.Replace (sPurtySql, " ORDER BY ", "\n\nORDER BY ", 1, -1, CompareMethod.Text);
sPurtySql = Strings.Replace (sPurtySql, " GROUP BY ", "\n\nGROUP BY ", 1, -1, CompareMethod.Text);
sPurtySql = Strings.Replace (sPurtySql, " VALUES", "\n\nVALUES", 1, -1, CompareMethod.Text);
sPurtySql = Strings.Replace (sPurtySql, " SET ", "\n\nSET ", 1, -1, CompareMethod.Text);

sPurtySql = Strings.Replace (sPurtySql, " and ", "\n\tand ", 1, -1, CompareMethod.Text);			
sPurtySql = Strings.Replace (sPurtySql, " or ", "\n\tor ", 1, -1, CompareMethod.Text);			

//change keywords to upper case
sPurtySql = Strings.Replace (sPurtySql, "SELECT ", "SELECT ", 1, -1, CompareMethod.Text);
sPurtySql = Strings.Replace (sPurtySql, "UPDATE ", "UPDATE ", 1, -1, CompareMethod.Text);
sPurtySql = Strings.Replace (sPurtySql, "DELETE ", "DELETE ", 1, -1, CompareMethod.Text);
sPurtySql = Strings.Replace (sPurtySql, "INSERT ", "INSERT ", 1, -1, CompareMethod.Text);

return sPurtySql;

}

Download... the code. (45 kb)





Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By Damian Eiff. Posted on 5/13/2006 6:42:09 PM
VB6.0 Version
Private Function SqlPretty(ByVal szCadenaSQL As String) As String

Dim sSQL As String
Dim asSQL() As String
Dim i As Integer
Let sSQL = szCadenaSQL
'//find and replace all the "," with carriage returns & commas
Let sSQL = Replace(sSQL, ", ", ",")
Let sSQL = Replace(sSQL, ",", "," & vbCrLf & vbTab)
'Let sSQL = Replace(sSQL, vbCr, " ")
Let sSQL = Replace(sSQL, vbCrLf, " ")
Let sSQL = Replace(sSQL, vbTab, " ")
Let sSQL = Replace(sSQL, " ", " ")
Let asSQL = Split(sSQL, " ")

For i = LBound(asSQL) To UBound(asSQL)
asSQL(i) = Trim(asSQL(i))
Next
Let sSQL = ""
Let sSQL = Join(asSQL, " ")

'//move FROM, WHERE, ORDER BY, GROUP BY down by 2 lines
Let sSQL = Replace(sSQL, " FROM ", vbCrLf & "FROM ")
Let sSQL = Replace(sSQL, " WHERE ", vbCrLf & "WHERE ")
Let sSQL = Replace(sSQL, " ORDER BY ", vbCrLf & "ORDER BY ")
Let sSQL = Replace(sSQL, " GROUP BY ", vbCrLf & "GROUP BY ")
Let sSQL = Replace(sSQL, " VALUES", vbCrLf & "VALUES")
Let sSQL = Replace(sSQL, " SET ", vbCrLf & "SET ")

Let sSQL = Strings.Replace(sSQL, " and ", vbCrLf & vbTab & "and ")
Let sSQL = Strings.Replace(sSQL, " or ", vbCrLf & vbTab & "or ")

'//change keywords to upper case
Let sSQL = Replace(sSQL, "select ", "SELECT ")
Let sSQL = Replace(sSQL, "update ", "UPDATE ")
Let sSQL = Replace(sSQL, "delete ", "DELETE ")
Let sSQL = Replace(sSQL, "insert ", "INSERT ")



Let SqlPretty = sSQL

End Function 'MakeSqlPurty(ByVal JumbledSql as String) As string