Greetings,
I would like to teach SQL Server via an UDF how to decode/encode my Revision code storage scheme. There are times when it would be helpful if SQL Server could perform the translation rather than rely on VBA code to perform the operation on each workstation.
The conversion involves encoding revision codes 1-99 followed by A-ZZ into a numeric datatype so that revisions will sort correctly, aka revision B is higher than revision A or revision 2.
Original working VBA code:
As far as I have gotten with translating it to SQL. I found that the SQL CASE operates very differently than VBA's CASE.
Anyone with suggestions how to successfully accomplish the VBA to SQL translation? TIA!
I would like to teach SQL Server via an UDF how to decode/encode my Revision code storage scheme. There are times when it would be helpful if SQL Server could perform the translation rather than rely on VBA code to perform the operation on each workstation.
The conversion involves encoding revision codes 1-99 followed by A-ZZ into a numeric datatype so that revisions will sort correctly, aka revision B is higher than revision A or revision 2.
Original working VBA code:
Code:
Public Function revconv_ConvA2N(ByVal strInput As String) As Integer
On Error GoTo Err_revconv_ConvA2N
Dim lLen As Long
Dim intN As Integer
Dim intR As Integer
lLen = Len(strInput)
If IsNumeric(strInput) Then
If (lLen = 1) Or (lLen = 2) Then
revconv_ConvA2N = Int(strInput)
Else
revconv_ConvA2N = -1
End If
Else
Select Case lLen
Case 0
revconv_ConvA2N = -1
Case 1
revconv_ConvA2N = 99 + (Asc(strInput) - 64)
Case 2
intN = 26 * (Asc(Mid(strInput, 1, 1)) - 64)
intR = Asc(Mid(strInput, 2, 1)) - 64
revconv_ConvA2N = 99 + (Int(Trim(Str(intN + intR))))
Case Else
revconv_ConvA2N = -1
End Select
End If
Exit_revconv_ConvA2N:
Exit Function
Err_revconv_ConvA2N:
Call errorhandler_MsgBox("Module: modshared_revconv, Function: revconv_ConvA2N()")
revconv_ConvA2N = 0
Resume Exit_revconv_ConvA2N
End Function
Code:
-- Define name of USER-DEFINED FUNCTION for this script
:setvar UDFNAME frevconv_ConvA2N
USE [Fandango]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Check if the USER-DEFINED FUNCTION exists already, DROP if found
IF EXISTS(SELECT * FROM [sys].[objects] WHERE [type_desc] = 'SQL_SCALAR_FUNCTION' AND [name] = '$(UDFNAME)')
DROP FUNCTION [dbo].[$(UDFNAME)]
GO
CREATE FUNCTION [dbo].[$(UDFNAME)] (
-- Add the parameters for the user-defined function here
@strInput AS varchar(2)
)
RETURNS smallint
AS
BEGIN
-- Insert statements for user-defined function here
DECLARE @intLen integer
DECLARE @sintN smallint
DECLARE @sintR smallint
@intLen = Len(@strInput)
IF IsNumeric(@strInput) THEN
IF (@intLen = 1) Or (@intLen = 2) THEN
RETURN = SmallInt(@strInput)
ELSE
RETURN = -1
END IF
ELSE
@intLen = Len(@strInput)
SELECT CASE @intLen
CASE 0
RETURN = -1
CASE 1
RETURN = 99 + (Asc(@strInput) - 64)
CASE 2
intN = 26 * (Asc(Substring(@strInput, 1, 1)) - 64)
intR = Asc(Substring(@strInput, 2, 1)) - 64
RETURN = 99 + (SmallInt(fTRIM(Str(intN + intR))))
CASE ELSE
RETURN = -1
END SELECT
END IF
END
Anyone with suggestions how to successfully accomplish the VBA to SQL translation? TIA!