Discussion Locked

SQL Server DTS - unusual transform

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

We're having trouble importing certain text files into SQL Server using DTS because numbers are stored in an unusual way. For example:

A positive integer is OK. For example. the number 152 is stored as 152 in the text files.

The decimal point is not included. For example, 152.79 is stored as 15279.

A negative number is stored with a special symbol instead of the final digit. For example, -152 is stored as 15K & -152.79 is stored as 1527R. These cannot be imported into a numeric field.

Here's the characters used for the final digit to signify a negative number.
} 0
J 1
K 2
L 3
M 4
N 5
O 6
P 7
Q 8
R 9

We need to fix any field that contains a negative sign. The data files contain hundreds of fields that must be checked. Ideally, we would create a custom function to do this. However, we do not know how to do this.

Here is how the function would be called. In this example, the custom function is named Fixnegative.

OutputNumber = Fixnegative(InputText)

The parameter named InputText is the text value to be checked & fixed. The output of the function is an integer. When the last character of the field is numeric, no change is required. When the last character of the field is one of the special characters listed above, then the result must include the correct final digit and be expressed as a negative number.

For example:

Fixnegative(152) returns 152
Fixnegative(15279) returns 15279
Fixnegative(15K) returns 152-
Fixnegative(1527R) returns 15279-

If we need to include a decimal position, we could do so as follows:

Fixnegative(15279) /100 returns 152.79
Fixnegative(1527R) / 100 returns 152.79-

I don't think that it matters whether the negative sign is leading or trailing, as long as SQL Server undertands that the number is negative.

Thanks

5 replies

by baronworks

I'm unsure of the focus of your inquiry, so please ask if anything is unclear.

In the Transform Data Task properties dialog, choose the transoformations tab, add a new transformation for the columns involved.

Choose to add an ActiveX Script.
Then, you should be able to do a simple string manipulation to fix the problem.

BTW: It looks like the real problem might be that someone is doing an improper export from a mainframe system, but I understand that these things are often out of ones control.

by stillatit

You might consider writing an external program in Visual Basic, C++ or some other language to do the conversions on a file by file basis and feed the resulting output files to SQL server.

The problem you are up against is that you need to pass what is effectively a string (character type) parameter to a function but want a numeric back. Easy to do in Basic, harder in SQL.

In Basic the guts of the function looks like:

Private Function myconvert(strInvalue As String) As Long
Dim myinstring As String
Dim lastchar As String
myinstring = strInvalue
If IsNumeric(myinstring) Then
myconvert = Val(myinstring)
Exit Function
End If

lastchar = Right(myinstring, 1)
If lastchar = "}" Then lastchar = "I"
myinstring = Left(myinstring , Len(myinstring ) - 1) + Chr(Asc(lastchar) - 25)
myconvert = -(Val(myinstring))
Exit Function

You will probably want a separate function for inserting the decimal.

If you really want to return a string and a trailing minus sign, you can do it this way:

Private Function myconvert(strInvalue As String) As String
Dim myinstring As String
Dim lastchar As String
myinstring = strInvalue
If IsNumeric(myinstring) Then
myconvert = myinstring
Exit Function
End If

lastchar = Right(myinstring, 1)
If lastchar = "}" Then lastchar = "I"
myinstring = Left(myinstring , Len(myinstring ) - 1) + Chr(Asc(lastchar) - 25) + "-"
myconvert = myinstring
Exit Function

Good luck.

by jjjjj

The answer initially looked promising, but we later rejected the idea of pre-processing the data outside of SQL Server.

This discussion is closed to new replies.