Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query Results to Text File

Posted on 2011-03-15
3
Medium Priority
?
389 Views
Last Modified: 2012-05-11
Is it possible to run a query and have results export to text file in pre-defined column widths with leading zeros if required?

I'm hoping I may be able to build a text file that I can import into a system.  It's 5 fields, all numeric, the results would need to look like this....

00000100002000000000300000000040000000005000000000

First field is 10,000
2nd is 2,000,000,000.....etc
0
Comment
Question by:tobin46
3 Comments
 
LVL 4

Expert Comment

by:qasim_md
ID: 35144316
Check the SQL - BCP QUERY OUT for your requirements.
0
 
LVL 9

Accepted Solution

by:
sshah254 earned 1400 total points
ID: 35144339
Use command line SQL - SQLCMD

Use the necessary padding functions from here - http://www.sqlusa.com/bestpractices2005/padleadingzeros/

and Concatenate your results.

Ss
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 600 total points
ID: 35144341
several options

SELECT RIGHT ( replicate('0', 10 ) + cast(10000 as varchar)  , 10 ) +  RIGHT ( replicate('0', 10 )+cat(secondcolumn as varchar)  , 10 ) + ..

if you want to build the file from you can use the osql utitlity

http://msdn.microsoft.com/en-us/library/aa214012(SQL.80).aspx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question