Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Function to Format Select Queries to Columns

By ButchH,

This function will strip all extra spaces, CRLFs and tabs from a SELECT statement, from the SELECT keyword to the End of the Order By clause, then reformat the statement to put all keywords at the left margin and subordinate clauses indented by ONE Tab.  There is an example of a SELECT statement that came from the Enterprise Manager Query Builder Grid, with the Final output shown after that.  The function is invoked with:  print dbo.ufx_fixcommas(' ') with the Select statement between the single quotes.  Any single quoted parameters MUST have TWO single quotes before running the Print command.  The function strips the double quotes and leave the single quote in place.  I usually use the REPLACE function first to replace ' with '' then add the print dbo.ufx_fixcommas(' before the SELECT keyword then close the SELECT statement with ')  This is not as fancy as I would like, but it's a lot quicker than manually formatting SQL statements.

Total article views: 136 | Views in the last 30 days: 1
 
Related Articles
FORUM

two DDL statements and SELECT in single batch

two DDL statements and SELECT in single batch will not work any workarounds

FORUM

select statement

select statement with case

FORUM

select statements

select statements

FORUM

select statement

select statement

FORUM

function in a sql statement

function in a sql statement

Tags
miscellaneous    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones