using SUBSTRING with NULL

  • I am getting this error when @ReferenceRange is null. 

    "Invalid length parameter passed to the SUBSTRING function.  The statement has been terminated."

     

    CASE

    WHEN @ReferenceRange IS NULL THEN NULL

    ELSE LEFT(@ReferenceRange , CHARINDEX('-', @ReferenceRange )-1)

    END,

    CASE

    WHEN @ReferenceRange IS NULL THEN NULL

    ELSE RIGHT(@ReferenceRange , LEN(@ReferenceRange ) - CHARINDEX('-', @ReferenceRange ))

    END,

     

    How do I get around this?  Do I have to push the parsing to a function where I do a null test first?u

  • Sounds like maybe it isn't null but maybe blank. Try changing both lines to this

     

    WHEN NullIf(@ReferenceRange,'') IS NULL THEN NULL

  • It worked, thx!  Is NULLIF new in 2005?

  • No. It is also available in 2000.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply