--:SetVar Function "AUX:DaysInMonth.ByDate"
if exists( select * from [sysobjects] (nolock) where [type] in ('FN','TF') and [name]=N'AUX:DaysInMonth.ByDate' )
   drop function [AUX:DaysInMonth.ByDate]
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:DaysInMonth.ByDate]( @Date datetime2 )
returns int
BEGIN
/*==================================================================================================
# SQLAUX scripting library | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# DETERMINING NUMBER OF DAYS IN MONTH (BY DATE VALUE):
#
# === Usage: ===
# dbo.[AUX:DaysInMonth.ByDate]( <Date> )
#
==================================================================================================*/

if @Date is null return null

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

declare @AbsoluteMonthCount int -- from beginning of year 1900
set @AbsoluteMonthCount = DATEDIFF( MONTH, 0, @Date )

return DATEDIFF( DAY,
   DATEADD( MONTH, @AbsoluteMonthCount, 0 ),
   DATEADD( MONTH, @AbsoluteMonthCount + 1, 0 ))

/*================================================================================================*/
END
GO
/*
select dbo.[AUX:DaysInMonth.ByDate]( GETDATE() ) 'Days in current month'
*/
