--:SetVar Function "AUX:FormatNumByWidth"
if exists( select * from [sysobjects] (nolock) where [type] in ('FN','TF') and [name]=N'AUX:FormatNumByWidth' )
   drop function [AUX:FormatNumByWidth]
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:FormatNumByWidth]( @NumberAsString nvarchar(max),
                               @WidthForPadding int,
                               @ExpressiveDisplayNull bit = 0 )
returns varchar(max)
BEGIN
/*==================================================================================================
# SQLAUX scripting library | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# NUMBER FORMATTING BY WIDTH (WITH NUMBER ARGUMENT AS STRING):
# - LEFT COMPLEMENT WITH SPACES;
# - DENOTION OUT OF SPACE CHARACTERS AS “*”;
# - POSSIBLE DISPLAYING NULL-S AS REPLICATION OF “-” CHAR
#
# === Usage: ===
# dbo.[AUX:FormatNumByWidth]( <Number as string>,
#                             <Width for padding>,
#                             <Display null expressively> )
# dbo.[AUX:FormatNumByWidth]( <Number as string>, <Width for padding>, 0 )
# dbo.[AUX:FormatNumByWidth]( <Number as string>, <Width for padding>, 1 )
# dbo.[AUX:FormatNumByWidth]( <Number as string>, <Width for padding>, null )
# dbo.[AUX:FormatNumByWidth]( <Number as string>, <Width for padding>, default )
#
==================================================================================================*/

if @WidthForPadding is null or @WidthForPadding < 0
   return null
if @WidthForPadding = 0
   return ''

if @NumberAsString is null
   if @ExpressiveDisplayNull is not null
      return REPLICATE
      (
         case @ExpressiveDisplayNull
            when 1 then '-'
            else ' '
         end,
         @WidthForPadding
      )
   else
      return null

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

declare @String nvarchar(max), @Length int
set @String = dbo.[AUX:PadStart](
   TRIM( @NumberAsString ),
   @WidthForPadding )
set @Length = LEN( @String )

return
   case
      when @Length <= @WidthForPadding then @String
      else '*' +
         case
            when @WidthForPadding > 1
            then SUBSTRING( @String, @Length-@WidthForPadding+2, @WidthForPadding-1 )
            else ''
         end
   end

/*================================================================================================*/
END
GO
/*
select '“'+dbo.[AUX:FormatNumByWidth]( '123456789', 10, default )+'”' 'Number as string by width'
select '“'+dbo.[AUX:FormatNumByWidth]( '123456789', 5, default )+'”' 'Number as string by width'
select '“'+dbo.[AUX:FormatNumByWidth]( '', 5, default )+'”' 'Number as string by width'
select '“'+dbo.[AUX:FormatNumByWidth]( null, 5, default )+'”' 'Number as string by width'
select '“'+dbo.[AUX:FormatNumByWidth]( null, 5, null )+'”' 'Number as string by width'
select '“'+dbo.[AUX:FormatNumByWidth]( null, 5, 0 )+'”' 'Number as string by width'
select '“'+dbo.[AUX:FormatNumByWidth]( null, 5, 1 )+'”' 'Number as string by width'
*/
