Tuesday, April 10, 2012

Exporting Queries from SQL to Excel

The easiest way to export a table from SQL is just running the query then selecting all of the results and copying it with headers then pasting it to excel. Sometimes that does not work though, and here is a workaround I have found if that does not work.

After writing the query, lets say "SELECT * from Contact.dbf" in SQL Server Management Studio (SSMS) follow these steps.


  • Create a text file on your computer, lets say output.txt
  • In SSMS Query > Results To... > Results to File
  • In SSMS Tools > Options... > Query Results > SQL Server > Results to Text
    • Next to output format select 'Custom Delimiter'
    • Next to custom delimiter you can put anything, but I have found that the pipe symbol works well, so I enter this symbol: |
    • Click OK
  • In SSMS right click on your database and select 'New Query' the write your query and select Run Query
    • IMPORTANT: the steps above must be completed before you open up the new query window.
  • Locate your file on your computer and select it then press ok (You will have to change the option to all files rather than the .rpt file it wants)
  • Finally open up excel and open up the text file, you will be prompted with a screen where you can set the | as the delimiter, and that's it!


Below is a link to the thread that assisted my in this.

http://stackoverflow.com/questions/6354130/is-there-a-select-into-outfile-equivalent-in-sql-server-management-studio

No comments:

Post a Comment