CREATE FUNCTION [dbo].VAT ( @Net_Amount [money], @Order_Date [date] ) RETURNS moneyAS BEGIN DECLARE @VAT_Rate [real] = CASE WHEN @Order_Date >= '2011-01-04' then 0.200 WHEN @Order_Date >= '2010-01-01' then 0.175 WHEN @Order_Date >= '2008-12-01' then 0.150 WHEN @Order_Date >= '1991-01-01' then 0.175 WHEN @Order_Date >= '1979-06-18' then 0.150 WHEN @Order_Date >= '1974-07-01' then 0.080 WHEN @Order_Date >= '1973-04-01' then 0.100 ELSE 0.000 END RETURN ( @Net_Amount * @VAT_Rate ); ENDGO

CREATE FUNCTION [dbo].VAT ( @Net_Amount [money], @Order_Date [date] ) RETURNS moneyAS BEGIN DECLARE @VAT_Rate DECIMAL(25,13) = CASE WHEN @Order_Date >= '2011-01-04' then 0.200 WHEN @Order_Date >= '2010-01-01' then 0.175 WHEN @Order_Date >= '2008-12-01' then 0.150 WHEN @Order_Date >= '1991-01-01' then 0.175 WHEN @Order_Date >= '1979-06-18' then 0.150 WHEN @Order_Date >= '1974-07-01' then 0.080 WHEN @Order_Date >= '1973-04-01' then 0.100 ELSE 0.000 END RETURN CONVERT( DECIMAL(25,13), @Net_Amount ) * @VAT_Rate ENDGO

IF OBJECT_ID (N'[dbo].TESTFunction', N'FN') IS NOT NULL DROP FUNCTION [dbo].[TESTFunction];GOCREATE FUNCTION [dbo].TESTFunction (@Gross_Amount [money]) RETURNS moneyAS BEGIN RETURN @Gross_Amount/1.15 ENDGODECLARE @Gross [money] = 48.0000;DECLARE @Test1 [money] = (@Gross/1.15)*0.26*0.9; -- Sets 9.7670DECLARE @Test2 [money] = dbo.TESTFunction(@Gross)*0.26*0.9; -- Sets 9.7669SELECT @Test1, @Test2;

CREATE FUNCTION [dbo].VAT2 ( @Net_Amount DECIMAL(25,13), @Order_Date [date] ) RETURNS TABLE WITH SCHEMABINDINGAS RETURN( SELECT @Net_Amount * CAST(CASE WHEN @Order_Date >= '2011-01-04' then 0.200 WHEN @Order_Date >= '2010-01-01' then 0.175 WHEN @Order_Date >= '2008-12-01' then 0.150 WHEN @Order_Date >= '1991-01-01' then 0.175 WHEN @Order_Date >= '1979-06-18' then 0.150 WHEN @Order_Date >= '1974-07-01' then 0.080 WHEN @Order_Date >= '1973-04-01' then 0.100 ELSE 0.000 END as DECIMAL(25,13)) AS VatAmt)GO