Link to home
Create AccountLog in
Avatar of austerhaus
austerhaus

asked on

Strip leading / trailing whitespace from strings in DTS Visual Basic Transformation script

Hello,

I have the attached ActiveX script that imports data into my table. The problem I am encountering is that the source data (a text file) contains leading a ASCII character 160 at the start of each value. Unfortunately, the TRIMs do not remove these characters. Is it possible to get rid of these in the transformation script? If so, how? I am a novice when it comes to ActiveX, so detailed assistance is appreciated. Please let me know if any additional detail is required.
'**********************************************************************
'  Visual Basic Transformation Script
'  Copy each source column to the
'  destination column
'************************************************************************
 
Function Main()
	DTSDestination("run_dt") = TRIM(CDATE(DTSSource("Col001")))
	DTSDestination("region_cd") = UCASE(TRIM(DTSSource("Col002")))
	DTSDestination("location_cd") = TRIM(DTSSource("Col003"))
	DTSDestination("area_cd") = UCASE(TRIM(DTSSource("Col004")))
	DTSDestination("rep_id") = UCASE(TRIM(DTSSource("Col005")))
	DTSDestination("order_num") = TRIM(DTSSource("Col006"))
	DTSDestination("order_type") = UCASE(TRIM(DTSSource("Col007")))
	DTSDestination("mobile_num") = TRIM(DTSSource("Col008"))
	DTSDestination("manager_id") = UCASE(TRIM(DTSSource("Col009")))
	DTSDestination("e_cd") = UCASE(TRIM(DTSSource("Col010")))
	DTSDestination("item_cd") = UCASE(TRIM(DTSSource("Col011")))
	DTSDestination("item_dt") = TRIM(CDATE(DTSSource("Col012")))
	DTSDestination("customer_name") = UCASE(TRIM(DTSSource("Col013")))
	DTSDestination("add_offer") = UCASE(TRIM(DTSSource("Col014")))
	DTSDestination("disc_amt") = TRIM(DTSSource("Col015"))
	DTSDestination("retail_amt") = TRIM(DTSSource("Col016"))
	DTSDestination("remarks") = LCASE(TRIM(DTSSource("Col017")))
	DTSDestination("disc_desc") = UCASE(TRIM(DTSSource("Col018")))
	Main = DTSTransformStat_OK
End Function

Open in new window

Avatar of Steve Hogg
Steve Hogg
Flag of United States of America image

You could also use the REPLACE function to change the unwanted char to an empty string.
ASKER CERTIFIED SOLUTION
Avatar of Steve Hogg
Steve Hogg
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of austerhaus
austerhaus

ASKER

Thank you for the fast reply. It seems that there are errors thrown when a value is null. However, I was able to get around that using:

IF ISNULL(DTSSource("Col008").value) THEN
        DTSDestination("mobile_num") = "0000000000"
    ELSE
        DTSDestination("mobile_num") = REPLACE(TRIM(DTSSource("Col008")), CHR(160), "")
        END IF

Can you tell me if that is the optimal thing to do?
Is this optimal, i guess that depends on the number of records. It could be quicker to remove the spaces and unwanted characters in the transformation but this is fine.
I am not sure what you mean by "remove the spaces and unwanted characters in the transformation". The code I posted (see below) is contained in the transformation script. Am I missing something?

...
DTSDestination("customer_name") = REPLACE(UCASE(TRIM(DTSSource("Col013"))),Chr(160),"")
IF ISNULL(DTSSource("Col008").value) THEN
        DTSDestination("mobile_num") = "0000000000"
    ELSE
        DTSDestination("mobile_num") = REPLACE(TRIM(DTSSource("Col008")), CHR(160), "")
        END IF
....
Oh, sorry. I don't why I was thinking it was an ActiveX script. This is good the way you have it. I can't think of a better way.
Great, thank you for the assistance!