USE [DB]
GO
/****** Object: UserDefinedFunction [dbo].[EAN13] Script Date: 07/04/2017 15:21:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/**
--功能:獲取EAN-13碼的檢驗位
--Author:josy(百年樹人)
--引數@s:EAN-13碼前12位
**/
ALTER function [dbo].[EAN13](@s char(12))
returns varchar(1)
as
begin
return (10-(
(cast(substring(@s,2,1) as int)+substring(@s,4,1)+substring(@s,6,1)
+substring(@s,8,1)+substring(@s,10,1)+substring(@s,12,1)
)*3
+substring(@s,1,1)+substring(@s,3,1)+substring(@s,5,1)
+substring(@s,7,1)+substring(@s,9,1)+substring(@s,11,1)
)%10)%10
end
create function dbo.EAN8 (@value varchar(8))
returns varchar(8)
as
begin
declare @s1 int ,@s2 int,@s3 int
declare @t table (id int identity(1,1),b bit)
insert into @t(b)
select top 8 1 from syscolumns
set @value='0'+reverse(@value)
select @s1=sum(cast(substring(@value,id,1) as int))
from @t a
where len(@value)>=id and id%2=0
set @s1=@s1*3
select @s2=sum(cast(substring(@value,id,1) as int))
from @t a
where len(@value)>=id and id>=3 and id%2=1
set @s3=right(@s1+@s2,1)
return left(reverse(@value),7)+ltrim(10-case @s3 when '0' then '10' else @s3 end)
end
go