Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Convert UPC-E to UPC-A upon insert with UDF

$
0
0

I have the function but I need this function to insert on the PosCode in my Products Table:

CREATE TABLE [Pricebook].[Products] (
    [PosCode]             [dbo].[posCode]        NOT NULL,
    [PosCodeFormat]       AS                     ([dbo].[udf_PosCodeFormat]([PosCode])),
    [CheckDigit]          AS                     ([dbo].[udf_Calculate_UPCA_CheckDigit]([PosCode])),

where the PosCode is the primary key.  If the the user enters 7 digits then it should convert it to the 11 digit format in that column.  I need the returned output of @upcA to overwrite the data entered in the PosCode column....Here is the function:

CREATE function dbo.udf_UPCE_to_UPCA (@PosCode varchar(50))
-- limiting to varchar(8) only reads first 8 characters of any longer strings passed in

returns varchar(12)
as
begin
--Function to take in UPC-E, calculate and return its UPC-A.

-- local variables
declare @main_six varchar(6) -- The 6 important (middle) digits of UPCE
declare @upcA varchar(12) -- Calculated UPCA

-- Iniial settings
set @PosCode = ltrim(rtrim(isnull(@PosCode,'')))
set @upcA = ''

-- Calculate UPC-A
if (-- Required conditions for conversion: length must be 8, must start with 0 or 1
len(@PosCode) = 8
and left(@PosCode,1) in ('0','1')
and isnumeric(@PosCode) = 1
)
begin
set @main_six = substring(@PosCode,2,6)

if (right(@main_six,1) in ('0','1','2'))
begin
set @upcA = left(@PosCode,1)
+ left(@main_six,2)+ right(@main_six,1)+ '0000'+ substring(@main_six,3,3)+ right(@PosCode,1)
end

else if (right(@main_six,1) = '3')
begin
set @upcA = left(@PosCode,1)
+ left(@main_six,3)+ '00000'+ substring(@main_six,4,2)+ right(@PosCode,1)
end

else if (right(@main_six,1) = '4')
begin
set @upcA = left(@PosCode,1)
+ left(@main_six,4)+ '00000'+ substring(@main_six,5,1)+ right(@PosCode,1)
end

else
begin
set @upcA = left(@PosCode,1)
+ left(@main_six,5)+ '0000'+ substring(@main_six,6,1)+ right(@PosCode,1)
end
end -- main if: Required conditions
return @upcA
end
Any help is greatly appreciated...thanks to all who offer it.

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>