I am new to writing SQL fucntions and trying to write a scalar function in T-SQL. The function should insert leading zeros into an input field of numbers the length of the input can vary in length, i,e,. 123, 1234567, 9876543210 and so forth. The input field is defined as varchar(13)
here is the code that I have written for the function:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION dbo.FN_ZeroPad
(
-- the parameters for the function here
@FN_Input_No varchar(13)
)
RETURNS varchar(13)
AS
BEGIN
declare @FN_PadZero varchar (13),
@Return varchar (13)
Set @FN_PadZero = '0000000000000'
select @Return =
Case
When @FN_Input_No is null then 'Missing'
When @FN_Input_No < = 0 then '0000000000000'
When @FN_Input_No > 0 then (@FN_PadZero + @FN_Input_No)
else null End
-- Return the result of the function
RETURN @return
End
I am not certain that I have defined the fields correctly as I am trying include both an Input variable and an output variable which is new to me. I am trying in insert leading zeros in front of input field and return the value inputted with leading zeros.