Solved

DTS - Vbscript transformation

Posted on 2000-04-12
14
1,123 Views
Last Modified: 2013-11-30
One column in a DTS package (Excel to SQL Server 7) contains
zipcode.   The column is defined as varchar 5 in the SQL Server
table. Any zero's in the first position of the zipcode get
truncated, so for example, 03341 would end up as 3341 in
the destination table.
What vbscript code could I use  to transform this correctly?
0
Comment
Question by:vd
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 2

Expert Comment

by:DawsonB
ID: 2708429
Try this

Function Main()
dim zipcode

  ZipCode = DTSDataSource("zipcode")
  ZipCode = string(5-len(ZipCode),"0") & ZipCode
  DTSDestination("zipcode") = ZipCode
  Main = DTSTransformStat_OK

End Function
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2708435
Sorry, that should have read:
 (difference in DTSSource line)

Function Main()
dim zipcode

  ZipCode = DTSSource("zipcode")
  ZipCode = string(5-len(ZipCode),"0") & ZipCode
  DTSDestination("zipcode") = ZipCode
  Main = DTSTransformStat_OK

End Function
0
 

Author Comment

by:vd
ID: 2708479
This looks great, but I'm getting errors, apparently
on NULL's  in the source zipcodes.  
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2708545
as a guss, try

Function Main()
dim zipcode

  ZipCode = DTSSource("zipcode")
  If Not Null(ZipCode) then
    ZipCode = string(5-len(ZipCode),"0") & ZipCode
  Else
    ZipCode = "" ' Or any "Default" value you prefer
  End If
  DTSDestination("zipcode") = ZipCode
  Main = DTSTransformStat_OK
End Function


This will put any entries in your source db in with an empty string in your destination table.

Hope this helps.


0
 

Author Comment

by:vd
ID: 2708546
Does the code above prefix every zipcode with
'O'? This wouldn't work if the zipcode does not have
zero in the first position.
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2708560
as an afterthought,
the lines

  Else
    ZipCode = Null
  End if

should work if you are allowing nulls in your destination table, but as a rule I never allow nulls in my db's unless there is no logical concept of a default value. For example, a field called BirthDay could have a default value of Null because if I don't know the true value, then some other value is more wrong than null, but
a Field of Name I would prefer an empty string ("" in vb, or '' in tsql) to Null.
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2708598
I am not familiar with US zip codes (assuming these are us?) but if all of the zip codes you are getting are supposed to be 5 digits long, this will put 0's in front of any other number, and leave you with zip codes all of 5 digits long. for examples the following would result.

Source        Destination
1243             01234
12345            12345
11               00011
9                00009
10000            10000


If you have zip codes that 'could' be shorter than 5 digits, then you may need to change the column properties in the Excel spreadsheet first to say a text, then manually adjust those zip codes which need a zero put on the front of them.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:vd
ID: 2708881
I'll try out your suggestions and back get to you.
No, actually the NULLS are on the input file, appear
to be blank records - I'll have to look at that.
0
 

Author Comment

by:vd
ID: 2709555
Doesn't work. An error "invalid use of NULL" occurs,even when I eliminate most everything but
the  "ZipCode = string(5-len(ZipCode),"0") & ZipCode" statement.  
Can I stop processing when it gets to the NULL
records toward the end of the spreadsheet,
like the traditional "end of file" processing?
0
 
LVL 6

Expert Comment

by:crsankar
ID: 2710765
try using a string function to convert your dtssource data

In oracle you can use to_char. I dont know the equivalent in vb.
0
 
LVL 6

Expert Comment

by:crsankar
ID: 2710777
I think it is ctsr in vb.
0
 
LVL 2

Accepted Solution

by:
DawsonB earned 50 total points
ID: 2710873
Crsankar: That might work, the correct format in vb is
ZipCode = Cstr(ZipCode)


0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2711083
Modify your line
ZipCode = DTSSource("zipcode")
into
ZipCode = DTSSource("zipcode").Value

Explanation:
VBScript is not really typed, and therefore has problems with the default properties
0
 

Author Comment

by:vd
ID: 2744782
We were able to avoid the problem by redefining
the column in excel to TEXT.  The approach
described here will be useful in the future!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

757 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

16 Experts available now in Live!

Get 1:1 Help Now