Click here to Skip to main content
13,191,040 members (58,454 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a .txt file containing fixed length data of 533 in width e.g my file contain aaaaaaa....aa (junk data taken as example) now I want to store this data in table
eg Table1 (c1 char(1)
C2 Char(10),
C3 char(15),
C4 char(1),
Cx char(10)

Now how to split this flat file data so that (a) goes c1 of length 1 , 10 (a) goes to c2 column of length 10.

I tried using Flat file as source in SSIS but it only allow 285 width of column for fixed length data but my data is of 533 in width.

I was thinking of putting this data in temp table and then separate data using , and putting in table1.
But I need help with writing query to do this?
Posted 26-Sep-12 15:37pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

By default, the Flat File connection manager sets the length of string columns to 50 characters. In the Flat File Connection Manager Editor dialog box, you can evaluate sample data and automatically resize the length of these columns to prevent truncation of data or excess column width. Also, unless you subsequently resize the column length in a Flat File source or a transformation, the column length of string column remains the same throughout the data flow. If these string columns map to destination columns that are narrower, warnings appear in the user interface. Moreover, at run time, errors may occur due to data truncation. To avoid errors or truncation, you can resize the columns to be compatible with the destination columns in the Flat File connection manager, the Flat File source, or a transformation. To modify the length of output columns, you set the Length property of the output column on the Input and Output Properties tab in the Advanced Editor dialog box.

If you update column lengths in the Flat File connection manager after you have added and configured the Flat File source that uses the connection manager, you do not have to manually resize the output columns in the Flat File source. When you open the Flat File Source dialog box, the Flat File source provides an option to synchronize the column metadata.

If you are not comfortable with it you can create your own text file reader with Script component: follow:[^]
using System.IO;
public class ScriptMain:
    private StreamReader textReader;
    private string exportedAddressFile;
    public override void AcquireConnections(object Transaction)
        IDTSConnectionManager100 connMgr = this.Connections.MyFlatFileSrcConnectionManager;
        exportedAddressFile = (string)connMgr.AcquireConnection(null);
    public override void PreExecute()
        textReader = new StreamReader(exportedAddressFile);
    public override void CreateNewOutputRows()
        string nextLine;
        string[] columns;
        char[] delimiters;
        delimiters = ",".ToCharArray();
        nextLine = textReader.ReadLine();
        while (nextLine != null)
            columns = nextLine.Split(delimiters);
                MyAddressOutputBuffer.AddressID = columns[0];
                MyAddressOutputBuffer.City = columns[3];
            nextLine = textReader.ReadLine();
    public override void PostExecute()
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use OPENROWSET()[^] and SUBSTRING()[^] as is shown in an example:

--create temporary table
CREATE TABLE #tt ([longText] NVARCHAR(1000))
--insert long data in to temporary table
FROM OPENROWSET ('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=E:\;', 'SELECT * FROM test533.txt');
--get data from temporary table
SELECT SUBSTRING([longText], 1, 1) AS C1, SUBSTRING([longText], 2, 10) AS C2, SUBSTRING([longText], 11, 15) AS C3, 
		SUBSTRING([longText], 25, 1) AS C4, SUBSTRING([longText], 26, 4) AS C5, SUBSTRING([longText], 30, 503) AS C6
FROM #tt
--remove temporary table

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171016.2 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100