Mogli Mogli - 3 months ago 8
SQL Question

Error in > or < symbol in sql?

My code is :

DECLARE @String VARCHAR(100)
SET @String = 'Before 91-150 days'

-- Chop off the end character
SET @String =
CASE @String
WHEN null THEN null
ELSE (CASE LEN(@String)
WHEN 0 THEN @String
WHEN (charindex('-',(SUBSTRING(@String, charindex(' ', @String), CHARINDEX(' ', @String) - 3))) > 0) THEN --(shows error in >)
SUBSTRING(@String, charindex(' ', @String), CHARINDEX(' ', @String) - 4) - 1
WHEN (charindex('-', SUBSTRING(@String, charindex(' ', @String), CHARINDEX(' ', @String) - 3)) < 0) THEN --(shows error in <)
SUBSTRING(@String, charindex(' ', @String), CHARINDEX(' ', @String) - 3) - 1
END)
END

SELECT @String


Error is :


Msg 102, Level 15, State 1, Line 12

Incorrect syntax near '>'.


I don't know what I am doing wrong...

Thanks in advance.

Answer

Your cases don't make sense. The first one is when 0 but the second one is a boolean. Make them both use an int by moving the first LEN inside the WHEN:

CASE 
    WHEN LEN(@String) = 0 
        THEN @String          
    WHEN CHARINDEX('-', SUBSTRING(@String, charindex(' ',@String), CHARINDEX(' ',@String)-3), 0) > 0
        THEN --Other stuff
Comments