• You'll have better performance if you change the following statement

    WHERE SD.Code IS NOT NULL AND RTRIM(LTRIM(SD.Code ))<>''

    with

    WHERE SD.Code <> ''

    The results are the same and you can test them if you want.

    Here's a script.

    DECLARE @Tabletable(

    mystringchar(15))

    INSERT @Table

    VALUES( ''),( ' '),( ' '),

    ( 'a'),( ' b'),( 'c '),

    ( ' '), (NULL)

    SELECT * FROM @Table

    WHERE mystring <> ''

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2