# SQL Select or ADO Iterator to get output to a file, nicely formatted

**URL:** <https://www.finalbuilder.com/forums/t/sql-select-or-ado-iterator-to-get-output-to-a-file-nicely-formatted/4572>\
**Category:** Discussion\
**Created:** [July 7, 2014, 10:51am UTC](https://www.finalbuilder.com/forums/t/sql-select-or-ado-iterator-to-get-output-to-a-file-nicely-formatted/4572 "2014-07-07T10:51:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathanmeltzer](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jonathanmeltzer](https://www.finalbuilder.com/forums/u/jonathanmeltzer)\
**Post date:** [July 7, 2014, 10:51am UTC](https://www.finalbuilder.com/forums/t/sql-select-or-ado-iterator-to-get-output-to-a-file-nicely-formatted/4572/1 "2014-07-07T10:51:24Z")

</div>

What I am trying to do seems basic: &nbsp;Select a bunch of fields from a database table, and export them to a text file with a delimiter (comma, pipe, whatever). &nbsp;However, I cannot get this to work without doing the following set of steps:  
  
1. Define the query in ADO Iterator  
2. Define a variable for each field I am selecting  
3. Assign each field to a variable  
4. Trim every variable separately (left and right trim)  
5. Write each record to the file by specifying the full line (variables and delimiters).  
  
This is fine for a few fields, but I have one file that has well over 30 fields in it. &nbsp;  
  
I tried doing a MSSQL Select and sending the output to a file, but there was no way to trim the fields before sending the output through to the file, and there was no way (that I found) to make the column widths variable in the file (i.e. have each column value take up space equal to its length, rather than have every field use a predetermined amount of space).  
  
Please tell me I am missing something. &nbsp;Creating 30+ variables, then trimming 30+ fields one at a time, then specifying the entire line using variables and delimiters when all I want to do is to send the selected output to the file directly seems like a large hassle and one that will slow down the process considerably.  
  
Any help/guidance is appreciated.  
  
Thanks,  
  
Jonathan

---

<div class="post-metadata">

**Author:** ![nevar](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@nevar](https://www.finalbuilder.com/forums/u/nevar)\
**Post date:** [July 8, 2014, 1:00am UTC](https://www.finalbuilder.com/forums/t/sql-select-or-ado-iterator-to-get-output-to-a-file-nicely-formatted/4572/2 "2014-07-08T01:00:39Z")

</div>

Hi Jonathan,   
  
 In the select query format the fields how you want at this stage into one combined field. The single column from the select statement then can be read into a variable and written to a file.   
  
 There are a number of string functions in SQL, such as Rtrim and Ltrim. The trick will be worrying about nulls, which you would have to handled in the FinalBuilder script as well.

---

<div class="post-metadata">

**Author:** ![jonathanmeltzer](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jonathanmeltzer](https://www.finalbuilder.com/forums/u/jonathanmeltzer)\
**Post date:** [July 8, 2014, 9:04am UTC](https://www.finalbuilder.com/forums/t/sql-select-or-ado-iterator-to-get-output-to-a-file-nicely-formatted/4572/3 "2014-07-08T09:04:40Z")

</div>

This solution seems to work pretty well, as long as I look for the NULL values when forming the string. Thank you for the help!
