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 <> ''