Concatenating null values to a string will return a null string in Sql server.
The solution is usually to use Sql server's built in function to convert null values to empty strings, so your code would look like this:
REPLACE(COALESCE(STR(temp.AdminTestId,5), ''),' ','0') + '-' +
REPLACE(COALESCE(STR(A.UserTestId,5), ''),' ','0') + '-' +
REPLACE(COALESCE(STR(A.Sequence,2), ''), ''),' ','0') as TestId,
The COALESCE function is Sql server will return the first argument it receives that is not NULL
.
Edit
To answer your comment, taking advantage of the fact that concatenating null to a string will return null, you can simply concatenate the hyphen to A.Sequence
:
REPLACE(COALESCE(STR(temp.AdminTestId,5), ''),' ','0') + '-' +
REPLACE(COALESCE(STR(A.UserTestId,5), ''),' ','0') +
REPLACE(COALESCE('-' + STR(A.Sequence,2), ''),' ','0') as TestId,