﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [C6MartPeriodico].[getDescFromDate] 
(
	@Durata INT
)
RETURNS VARCHAR(128)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @Desc VARCHAR(128)
	DECLARE @Anni INT
	DECLARE @Mesi INT
	DECLARE @AnniDesc VARCHAR(64)
	DECLARE @MesiDesc VARCHAR(64)
	SET @Desc = ''
	SET @Anni = @Durata / 12
	SET @Mesi = @Durata - (@Anni * 12)
	SET @AnniDesc = ''
	SET @MesiDesc = ''
	IF @Anni > 0
		IF @Anni = 1
			SET @AnniDesc = @AnniDesc + '1 anno'
		ELSE
			SET @AnniDesc = @AnniDesc + CAST(@Anni AS VARCHAR) + ' anni'
	IF @Mesi > 0
		IF @Mesi = 1
			SET @MesiDesc = @MesiDesc + '1 mese'
		ELSE
			SET @MesiDesc = @MesiDesc + CAST(@Mesi AS VARCHAR) + ' mesi'
	IF LEN(@AnniDesc) = 0
		SET @Desc = @MesiDesc
	ELSE
		IF LEN(@MesiDesc) = 0
			SET @Desc = @AnniDesc
		ELSE
			SET @Desc = @AnniDesc + ' e ' + @MesiDesc
	-- Return the result of the function
	RETURN @Desc
END