Solved

Query Results to Text File

Posted on 2011-03-15
3
377 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 350 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 150 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 R2 - stored prodecure with execution plan make faster 24 115
Stored Procedure 2 47
Can Unique column have more than one Null? 8 48
SQL Server stored proc 2 13
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now