Thanks for the replies. That is helpful. The AM/PM doesn't use a space after the time, so I revised as below. Not sure why the '0' + was added since it was always removed by the outer function.
IF (@dateformat = 7)
begin
set @converteddt =
STUFF(right( convert(varchar,@datevar,100 ) ,7), 6, 0, ' ')
set @formatexists = 1
end