/****** Object:  UserDefinedFunction [dbo].[MyStr]    Script Date: 03/11/1404 04:53:15 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

--select Str(30,30,4)
ALTER  FUNCTION [dbo].[MyStr]
(@Num BIGINT, @Len INT) RETURNS nvarchar(30)
WITH SCHEMABINDING
AS
BEGIN
  DECLARE @RES as nvarchar(20)
  SET @RES = RTRIM(LTRIM(STR(@Num)))
  if Len(@RES) < @Len 
  BEGIN
     SET @RES=REPLICATE('0', @Len-Len(@RES))+@RES
  END
  RETURN @RES
END
