if exists(select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'udf_Calculate_UPCA_CheckDigit' ) begin drop function dbo.udf_Calculate_UPCA_CheckDigit end go create function dbo.udf_Calculate_UPCA_CheckDigit ( @PosCode varchar(20) ) returns varchar(1) as begin /************************************************************************** 2013/01/15 Hakim Ali Function returns the Check Digit when passed in first 11 characters of UPC-A. **************************************************************************/ -- Local variables declare @checkdigit varchar(1) declare @sum_of_odds int declare @sum_of_evens int -- Initial settings set @checkdigit = '' set @PosCode = ltrim(rtrim(isnull(@PosCode,''))) set @sum_of_odds = 0 set @sum_of_evens = 0 -- Calculate CheckDigit if (len(@PosCode) = 11 and isnumeric(@PosCode) = 1) begin set @sum_of_odds = convert(int,substring(@PosCode,1,1)) + convert(int,substring(@PosCode,3,1))+ convert(int,substring(@PosCode,5,1))+ convert(int,substring(@PosCode,7,1))+ convert(int,substring(@PosCode,9,1))+ convert(int,substring(@PosCode,11,1)) set @sum_of_evens = convert(int,substring(@PosCode,2,1))+ convert(int,substring(@PosCode,4,1))+ convert(int,substring(@PosCode,6,1))+ convert(int,substring(@PosCode,8,1))+ convert(int,substring(@PosCode,10,1)) if ((((@sum_of_odds * 3) + (@sum_of_evens)))%10 = 0) begin set @checkdigit = '0' end else begin set @checkdigit = convert(varchar(1),(10 - (((@sum_of_odds * 3) + (@sum_of_evens)))%10)) end end return @checkdigit end -- end create function go
Code courtesy from sqlzen but I must be doing something wrong cause it does not insert the 12 digit.
I have a table Products and Column PosCode(yes I changed all upca_11 to PosCode
Do i need a stored proc??? this is my first function...
Thanks ahead for any help offered and sorry if I sound like rookie,...I'm just learning..