﻿

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[fis_admin_getUserAddressForm]
(
	@name_enc varbinary(260)
	,@title_enc varbinary(50)
	,@gender_enc varbinary(100)
	,@language varchar(2)
)
RETURNS nvarchar(255)
AS
BEGIN
	DECLARE @Username nvarchar(255);
	DECLARE @l varchar(2) = LOWER(ISNULL(@language, 'de'));
	DECLARE @g varchar(1) = LOWER(ISNULL(CAST(DECRYPTBYKEY(@gender_enc) as varchar(1)), 'm'));

	SET @Username = CAST(		CASE WHEN @l = 'de' THEN 'Sehr geehrte'  + CASE WHEN @g = 'm' THEN 'r Herr ' ELSE ' Frau ' END
									 ELSE                'Dear '		 + CASE WHEN @g = 'm' THEN 'Mr ' ELSE ' Ms ' END
								END
								
								+ ISNULL(CASE WHEN @title_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@title_enc) as varchar(50)) + ' ' END,'')
								+ ISNULL(CASE WHEN @name_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@name_enc ) as nvarchar(100)) END, '')
									as nvarchar(255)
						);

	RETURN @username;

END