(Using SQL Server 2005)
Prepare to cringe: I'm brand new to SQL and am working on my third scalar-valued function.
I'm trying to strip all semicolons [;] and greater than signs [>] from a string, but for some reason the following condition is evaluating as FALSE pretty much no matter what input is provided. (@Counter is a counter running from 1 to the length of @Victim; the idea is to look at each character and remove it if it's a semicolon or greater than sign.)
Any clues what I'm doing wrong?
Full code:
Prepare to cringe: I'm brand new to SQL and am working on my third scalar-valued function.
I'm trying to strip all semicolons [;] and greater than signs [>] from a string, but for some reason the following condition is evaluating as FALSE pretty much no matter what input is provided. (@Counter is a counter running from 1 to the length of @Victim; the idea is to look at each character and remove it if it's a semicolon or greater than sign.)
Code:
((SUBSTRING(@Victim, @Counter, 1) <> ';') AND (SUBSTRING(@Victim, @Counter, 1) <> '>'))
Full code:
Code:
ALTER FUNCTION dbo.ScrubDenialCode
(@Victim as varchar(255))
RETURNS varchar(255)
AS
BEGIN
DECLARE @Counter As Integer
DECLARE @DenialCode As varchar(255)
DECLARE @DenialCode2 As varchar(255)
DECLARE @ScrubbedVictim As varchar(255)
DECLARE @ScrubDenialCode As varchar(255)
SELECT @Counter = 1
WHILE @Counter <= Len(@Victim)
BEGIN
IF ((SUBSTRING(@Victim, @Counter, 1) <> ';') AND (SUBSTRING(@Victim, @Counter, 1) <> '>'))
BEGIN
SELECT @ScrubbedVictim = @ScrubbedVictim + SUBSTRING(@Victim, @Counter, 1)
END
SELECT @Counter = @Counter + 1
END
SELECT @Victim = @ScrubbedVictim
If Len(@Victim) = 0 Or @Victim IS NULL Or CONVERT(int, Len(@Victim) / 3) <> Len(@Victim) / 3
BEGIN
SELECT @ScrubDenialCode = ''
END
ELSE
BEGIN
SELECT @DenialCode = Left(@Victim, 3)
If Len(@Victim) > 3 --CHECK FOR MULTIPLE DENIAL CODES
BEGIN
SELECT @Counter = 1
WHILE @Counter <= Len(@Victim) / 3 - 1
BEGIN
SELECT @DenialCode2 = SUBSTRING(@Victim, 3 * @Counter + 1, 3)
If @DenialCode2 <> @DenialCode
BEGIN
If @DenialCode2 <> 'OVB'
BEGIN
If @DenialCode = 'OVB'
BEGIN
SELECT @DenialCode = @DenialCode2 --REPLACE OVB WITH NEW CODE
END
ELSE
BEGIN
SELECT @ScrubDenialCode = ''
GoTo THEEND
END
END
END
END
SELECT @ScrubDenialCode = @DenialCode
SELECT @Counter = @Counter + 1
END
ELSE
BEGIN
SELECT @ScrubDenialCode = @DenialCode
END
END
THEEND:
RETURN @Victim
--RETURN @ScrubDenialCode
END