CREATE   FUNCTION [dbo].[Crea_positionID_INLINE_20240427]
(
    @TipoProdotto VARCHAR(5)='',
    @CodInterno VARCHAR(13)='', 
    @CodMAF VARCHAR(13)='', 
    @CodISIN VARCHAR(16)='', 
    @CodSottoprodotto VARCHAR(13)='',
    @CodiceContratto VARCHAR(11)='',
    @NumeroPolizza VARCHAR(11)= '', 
    @Conto VARCHAR(12)= '',
    @Rubricato VARCHAR(4)='',
    @CustodiaGaranzia VARCHAR(1)='',
    @Termid VARCHAR(8)='', 
    @Anno DECIMAL(3)=0, 
    @Prog DECIMAL(9)=0
)
RETURNS TABLE
AS
RETURN
(
    SELECT
        CONCAT(
            RIGHT('000000000000' + ISNULL(@CodiceContratto, 'e'), 11),
            RIGHT('000000000000' + ISNULL(@Conto, 'e'), 12),
            RIGHT('000' + ISNULL(@Rubricato, 'e'), 3),
            RIGHT('0' + ISNULL(@CustodiaGaranzia, 'e'), 1),
            RIGHT('00000000000' + ISNULL(@NumeroPolizza, 'e'), 11),
            RIGHT('00000000' + ISNULL(@Termid, 'e'), 8),
            RIGHT('000' + CAST(ISNULL(@Anno, 0) AS VARCHAR(3)), 3),
            RIGHT('000000000' + CAST(ISNULL(@Prog, 0) AS VARCHAR(9)), 9)
        ) AS PositionID
)