SQL Server function to convert a string to uppercase and trim, if not null.

create function dbo.TrimAndUpperCaseIfNotNull(
    @val varchar(255) = null
)
returns varchar(255)
as
begin
    if @val is null
    begin
        return @val;
    end;

    return ltrim(rtrim(upper(@val)));
end;
go