Reputation: 9950
Am I the only person who thinks SSIS is a piece of *?
I have a query:
SELECT * FROM Table1
And I want to do is put this into a text file with specific lengths in the destination file (I have specs from a client).
For example, Field 1 size: 20, field 2 size: 3, and so on.
I also have the field positions, Field 1: 1-20, Field 2: 21-23, etc.
I created an SSIS package that has a source as a SQL query and a flat file as the destination. I CANNOT FOR THE LIFE OF ME GET THIS TO WORK.
The data on the text file is all messed up (its scattered all over the place, no columns).
This is the simplest task in the world and I cannot do it. I don't know if it's me or if SSIS is just a piece of crap.
Upvotes: 1
Views: 16397
Reputation: 9
To adjust Jeff's procedure to handle nulls, make these replacements. Instead of:
SET @InputWidth = LEN(@Input)
Use:
SET @InputWidth = Isnull(@Input,REPLICATE(' ',@OutputWidth)
And instead of:
SET @Output = @Input
Use:
SET @Output = Isnull(@Input,REPLICATE(' ',@OutputWidth)
Upvotes: 0
Reputation: 11
I actually just did this last week. I wrote a function that does this then for each field I call the function.
Here is the function
/*
USE [Newton-Dev]
GO
/****** Object: UserDefinedFunction [dbo].[CharPad] Script Date: 1/10/2015 11:38:27 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Script : Character Padding Function; Assist with fixed width file creation
Version : 1.0 (01/08/2015)
Author : Jeffery Williams
*/
ALTER FUNCTION [dbo].[CharPad] (
@Input VARCHAR(255)
,@OutputWidth INT
,@OutputAlign VARCHAR(5)
,@PadCharacter CHAR(1) )
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @Output VARCHAR(255)
DECLARE @InputWidth INT
SET @InputWidth = LEN(@Input)
IF @InputWidth > @OutputWidth
BEGIN
IF @OutputAlign = 'LEFT'
BEGIN
SET @Output = LEFT(@Input,@OutputWidth)
END
IF @OutputAlign = 'RIGHT'
BEGIN
SET @Output = RIGHT(@Input,@OutputWidth)
END
END
IF @InputWidth < @OutputWidth
BEGIN
IF @OutputAlign = 'RIGHT'
BEGIN
SET @Output = REPLICATE(@PadCharacter, @OutputWidth - @InputWidth ) + @Input
END
IF @OutputAlign = 'LEFT'
BEGIN
SET @Output =@Input+ REPLICATE(@PadCharacter, @OutputWidth - @InputWidth )
END
END
IF @InputWidth = @OutputWidth
SET @Output = @Input
RETURN (@Output)
END
Here is the query that calls the function and provides the fixed width output:
SELECT --dbo.CharPad (DeltaLineId,8,'LEFT',' '), dbo.CharPad (DeltaElgId,8,'LEFT',' ')
dbo.CharPad (CARRIER_ID,6,'LEFT',' ')
,dbo.CharPad (GROUP_NUM,7,'RIGHT','0')
,dbo.CharPad (LEFT('0000' + SUB_GROUP_ID, 4),9,'LEFT',' ')
,dbo.CharPad (SVC_TYPE,1,'LEFT',' ')
,dbo.CharPad (FILLER_1,1,'LEFT',' ')
,dbo.CharPad (FILLER_2,5,'LEFT',' ')
,dbo.CharPad (RATE_CODE,2,'LEFT',' ')
,dbo.CharPad (FILLER_3,1,'LEFT',' ')
,dbo.CharPad (ELIG_CODE,1,'LEFT',' ')
,dbo.CharPad (EFF_DATE,8,'LEFT',' ')
,dbo.CharPad (TERM_DATE,8,'LEFT',' ')
,dbo.CharPad (SUBSC_SSN,9,'LEFT',' ')
,dbo.CharPad (INDIV_SSN,9,'LEFT',' ')
,dbo.CharPad (CHNG_SSN,9,'LEFT',' ')
,dbo.CharPad (REL_CODE,2,'LEFT',' ')
,dbo.CharPad (HIRE_DATE,8,'LEFT',' ')
,dbo.CharPad (DOB,8,'LEFT',' ')
,dbo.CharPad (REL_TYPE,1,'LEFT',' ')
,dbo.CharPad (FIRST_NAME,24,'LEFT',' ')
,dbo.CharPad (MID_NAME,24,'LEFT',' ')
,dbo.CharPad (LAST_NAME,24,'LEFT',' ')
,dbo.CharPad (GENDER,1,'LEFT',' ')
,dbo.CharPad (POP_DESC,5,'LEFT',' ')
,dbo.CharPad (ADR_LINE_1,30,'LEFT',' ')
,dbo.CharPad (ADR_LINE_2,30,'LEFT',' ')
,dbo.CharPad (CITY,30,'LEFT',' ')
,dbo.CharPad ([STATE],2,'LEFT',' ')
,dbo.CharPad (COUNTY_CODE,3,'LEFT',' ')
,dbo.CharPad (COUNTRY_CODE,3,'LEFT',' ')
,dbo.CharPad (ZIP,5,'LEFT',' ')
,dbo.CharPad (ZIP_EXT,4,'LEFT',' ')
,dbo.CharPad (FILLER_4,21,'LEFT',' ')
,dbo.CharPad (USER_DEFINED,30,'LEFT',' ')
,dbo.CharPad (WAIT_PERIOD,1,'LEFT',' ')
,dbo.CharPad (CAID,9,'RIGHT','0')
,dbo.CharPad (FILLER_5,9,'LEFT',' ')
FROM export.DeltaLine
*/
Upvotes: 1
Reputation:
Go to the Connection Manager you defined for your flat file. In the left box, select General and set the Format to "Fixed Width". Then in the left box, select Advanced and here you can set the OutputColumnWidth for each field. Lastly, in the left box, select Preview to verify the results before executing the SSIS package.
Hope this helps. Kosh
Upvotes: 1