--:SetVar Function "AUX:YMComparison"
if exists( select * from [sysobjects] (nolock) where [type] in ('FN','TF') and [name]=N'AUX:YMComparison' )
   drop function [AUX:YMComparison]
GO
/* Translated by $SQLTRANS utility (SQL-file technology). Method author: Sergei Y.Kitáev.
** Script translation date-time: 2022-11-27 00:29:27.24 (stamp of SQL-file generation). */
create function [AUX:YMComparison]( @Year_1 int, @Month_1 int=null,
                               @Year_2 int, @Month_2 int=null )
returns int
BEGIN
/*==================================================================================================
# SQLAUX scripting library | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# COMPARISON OF TWO YEAR-MONTHS:
#
# Comparison result
# :   -1 —— 1st year-month is lesser than 2nd year-month;
# :    1 —— 1st year-month is greater than 2nd year-month;
# :    0 —— 1st year-month is equal to 2nd year-month;
# : null —— correlation is undetermined (null arg(s) or incorrect YM)
#
# === Usage: ===
# dbo.[AUX:YMComparison]( <Year-1>, <Month-1>, <Year-2>, <Month-2> )
# dbo.[AUX:YMComparison]( <Year-1>, <Month-1>, <Year-2>, null )
# dbo.[AUX:YMComparison]( <Year-1>, null, <Year-2>, <Month-2> )
#
==================================================================================================*/

if @Year_1 is null or @Year_1<0 or
   @Year_2 is null or @Year_2<0 or
   @Month_1 not between 1 and 12 or
   @Month_2 not between 1 and 12
   return null

----------------------------------------------------------------------------------------------------

if @Year_1 < @Year_2 return -1
if @Year_1 > @Year_2 return  1

----------------------------------------------------------------------------------------------------

if @Month_1 is null or @Month_2 is null
   return null

return
   case
      when @Month_1 < @Month_2 then -1
      when @Month_1 > @Month_2 then  1
      else 0
   end

/*================================================================================================*/
END
GO
/*
select dbo.[AUX:YMComparison]( 2015,11, 2015,11   ) 'YM-comparison result'
select dbo.[AUX:YMComparison]( 2015,11, 2015,10   ) 'YM-comparison result'
select dbo.[AUX:YMComparison]( 2015,11, 2015,12   ) 'YM-comparison result'
select dbo.[AUX:YMComparison]( 2015,11, 2015,null ) 'YM-comparison result'
select dbo.[AUX:YMComparison]( 2015,11, 2016,null ) 'YM-comparison result'
*/
