this is ONLY for SQL Server 2014 and before. It performs also space trimming on the right, which you may skip it. The while condition is working because it takes advantage of LEN() "bug".
CREATE FUNCTION [dbo].[RTrimCharacter]
(
@Value NVARCHAR(4000),
@CharacterToTrim NVARCHAR(1)
)
RETURNS NVARCHAR(4000)
AS
BEGIN
-- define a temporary nvarchar
declare @temp nvarchar(4000)
-- copy the input
set @temp = @Value
-- rtrim it
set @temp = RTRIM(@temp)
while (@Value = @temp)
begin
-- set the original to temp
set @Value = @temp
-- trim it the last if the character
if RIGHT(@temp, 1) = @CharacterToTrim
set @temp = LEFT( @temp , LEN(@temp)-1)
-- additionally trim the spaces
set @temp = RTRIM(@temp)
end
RETURN @temp
END