--:SetVar Function "AUX:ToStringVS.DHP"
if exists( select * from [sysobjects] (nolock) where [type] in ('FN','TF') and [name]=N'AUX:ToStringVS.DHP' )
   drop function [AUX:ToStringVS.DHP]
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:ToStringVS.DHP]( @Number decimal(38,16) )
returns varchar(max)
BEGIN
/*==================================================================================================
# SQLAUX scripting library | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# NUMBER CONVERSION TO STRING, WITH VARIABLE PRECISION (“$(dt_HPDecimal_AUX)” TYPE):
#
# === Usage: ===
# dbo.[AUX:ToStringVS.DHP]( <Number> )
#
==================================================================================================*/

if @Number is null return null

declare @String varchar(max), @DotPos int
set @String = dbo.[AUX:ToString.DHP]( @Number )
set @DotPos = CHARINDEX( '.', @String )
if @DotPos <= 0 return null

declare @P int
set @P = dbo.[AUX:StrLength]( @String )
while @P > @DotPos and SUBSTRING( @String, @P, 1 )='0'
   set @P = @P - 1
if @P = @DotPos
   set @P = @P - 1
if @P = 0
   return null

return SUBSTRING( @String, 1, @P )

/*================================================================================================*/
END
GO
/*
select dbo.[AUX:ToStringVS.DHP]( 345178.21354 ) 'Number as string (variable prec.)'
select dbo.[AUX:ToStringVS.DHP]( 345178.2135400 ) 'Number as string (variable prec.)'
select dbo.[AUX:ToStringVS.DHP]( -8435.000 ) 'Number as string (variable prec.)'
select dbo.[AUX:ToStringVS.DHP]( -8435 ) 'Number as string (variable prec.)'
*/
