Calculate Count of true bits in binary type with t-sql - sql

I need to find how many true bit exists in my binary value.
example:
input: 0001101 output:3
input: 1111001 output:5

While both answers work, both have issues. A loop is not optimal and destructs the value. Both solutions can not be used in a select statement.
Possible better solution is by masking together as follows
select #counter = 0
+ case when #BinaryVariable2 & 1 = 1 then 1 else 0 end
+ case when #BinaryVariable2 & 2 = 2 then 1 else 0 end
+ case when #BinaryVariable2 & 4 = 4 then 1 else 0 end
+ case when #BinaryVariable2 & 8 = 8 then 1 else 0 end
+ case when #BinaryVariable2 & 16 = 16 then 1 else 0 end
+ case when #BinaryVariable2 & 32 = 32 then 1 else 0 end
+ case when #BinaryVariable2 & 64 = 64 then 1 else 0 end
+ case when #BinaryVariable2 & 128 = 128 then 1 else 0 end
+ case when #BinaryVariable2 & 256 = 256 then 1 else 0 end
+ case when #BinaryVariable2 & 512 = 512 then 1 else 0 end
This can be used in a select and update statement. It is also an order of magnitude faster. (on my server about 50 times)
To help you might want to use the following generator code
declare #x int = 1, #c int = 0
print ' #counter = 0 ' /*CHANGE field/parameter name */
while #c < 10 /* change to how many bits you want to see */
begin
print ' + case when #BinaryVariable2 & ' + cast(#x as varchar) + ' = ' + cast(#x as varchar) + ' then 1 else 0 end ' /* CHANGE the variable/field name */
select #x *=2, #c +=1
end
Also as further note: if you use a bigint or go beyond 32 bits it is necessary to cast like follows
print ' + case when #Missing & cast(' + cast(#x as varchar) + ' as bigint) = ' + cast(#x as varchar) + ' then 1 else 0 end '
Enjoy

DECLARE #BinaryVariable2 VARBINARY(10);
SET #BinaryVariable2 = 60; -- binary value is 111100
DECLARE #counter int = 0
WHILE #BinaryVariable2 > 0
SELECT #counter +=#BinaryVariable2 % 2, #BinaryVariable2 /= 2
SELECT #counter
Result:
4

I've left various debug selects in.
begin
declare #bin as varbinary(20);
declare #bitsSet as int;
set #bitsSet = 0;
set #bin = convert(varbinary(20), 876876876876);
declare #i as int;
set #i = 0
select LEN(#bin), 'Len';
while #i < LEN(#bin)
begin
declare #bit as varbinary(1);
set #bit = SUBSTRING(#bin, #i, 1);
select #bit, 'Bit';
declare #power as int
set #power = 0;
while #power < 8
begin
declare #powerOf2 as int;
set #powerOf2 = POWER(2, #power);
if #powerOf2 <> 0
set #bitsSet = #bitsSet + (#bit & #powerOf2) / #powerOf2; -- edited to add the divisor
select #power, #powerOf2;
set #power = #power + 1;
end;
select #bitsSet;
set #i = #i + 1;
end;
select #bitsSet, 'End'
end;
Cheers -

You can handle an arbitrary length binary value by using a recursive CTE to split the data into a table of 1-byte values and counting all of the bits that are true in each byte of that table...
DECLARE #data Varbinary(MAX) = Convert(Varbinary(MAX), N'We can count bits of very large varbinary values without a loop or number table if you like...');
WITH each ( byte, pos ) AS (
SELECT Substring(#data, Len(#data), 1), Len(#data)-1 WHERE Len(#data) > 0
UNION ALL
SELECT Substring(#data, pos, 1), pos-1 FROM each WHERE pos > 0
)
SELECT Count(*) AS [True Bits]
FROM each
CROSS JOIN (VALUES (1),(2),(4),(8), (16),(32),(64),(128)) [bit](flag)
WHERE each.byte & [bit].flag = [bit].flag
OPTION (MAXRECURSION 0);

From SQL Server 2022 you can just use SELECT BIT_COUNT(input)
expression_value can be
Any integer or binary expression that isn't a large object (LOB).
For integer expressions the result can depend on the datatype. e.g. -1 as smallint has a binary representation of 1111111111111111 (two's complement) and will have more bits set for int datatype.

Related

SQL: How to display rhombus shape based on given length of characters?

My requirement is to build a rhombus using ‘X’ symbol based on below conditions.
If the length is 2, then it should display
X
XXX
X
If the length is 3, then it should display
X
XXX
XXXXX
XXX
X
If the length is 5, then it should display 5 lines above and 5 lines below with spaces.
I tried writing code, but I’m unable to get lower half part and spaces. Can someone help me here?
declare #X nvarchar(1) = 'X'
declare #chars int = 5
declare #W nvarchar(100) = 1
while (#chars > 0)
begin
print replicate(#X,#W)
set #chars = #chars - 1
set #W = #W + 2
end
Output:
X
XXX
XXXXX
XXXXXXX
XXXXXXXXX
You can get spaces using SPACE function. I have used your approach and added some code which returns lower part as well.
DECLARE #X NVARCHAR(1) = 'X'
DECLARE #chars INT = 5
DECLARE #LowerPart INT = #chars
DECLARE #W NVARCHAR(100) = 1
WHILE (#chars > 0)
BEGIN
PRINT SPACE(#chars)+REPLICATE(#X,#W)
SET #chars = #chars - 1
SET #W = #W + 2
END
WHILE (#chars <= #LowerPart)
BEGIN
PRINT SPACE(#chars)+REPLICATE(#X,#W)
SET #chars = #chars + 1
SET #W = #W - 2
END
I've considered your example rhombus for 3. So, for a given length 5, there will be only 4 lines above and 4 lines below with my solution.
declare #displayCharacter nvarchar(1) = 'X'
declare #rhombusLength int = 5
declare #repetition int = 1
declare #topRow int = 1
declare #bottomRow int = 1
--Top rows
while (#rhombusLength > #topRow)
begin
print REPLICATE(' ', #rhombusLength - #topRow) + REPLICATE(#displayCharacter, #repetition)
set #topRow = #topRow + 1
set #repetition = #repetition + 2
end
--Middle row
print REPLICATE(#displayCharacter, #repetition)
--Bottom rows
while (#rhombusLength > #bottomRow)
begin
set #repetition = #repetition - 2
print REPLICATE(' ', #bottomRow) + REPLICATE(#displayCharacter, #repetition)
set #bottomRow = #bottomRow + 1
end

Fuzzy logic matching

So, I'm looking at implementing Fuzzy logic matching in my company and having trouble getting good results. For starters, I'm trying to match up Company names with those on a list supplied by other companies.
My first attempt was to use soundex, but it looks like soundex only compares the first few sounds in the company name, so longer company names were too easily confused for one another.
I'm now working on my second attempt using the levenstein distance comparison. It looks promising, especially if I remove the punctuation first. However, I'm still having trouble finding duplicates without too many false positives.
One of the issues I have is companies such as widgetsco vs widgets inc. So, if I compare the substring of the length of the shorter name, I also pickup things like BBC University and CBC University campus. I suspect that a score using a combination of distance and longest common substring may be the solution.
Has anyone managed to build an algorithm that does such a matching with limited false positives?
We have had good results on name and address matching using a Metaphone function created by Lawrence Philips. It works in a similar way to Soundex, but creates a sound/consonant pattern for the whole value. You may find this useful in conjunction with some other techniques, especially if you can strip some of the fluff like 'co.' and 'inc.' as mentioned in other comments:
create function [dbo].[Metaphone](#str as nvarchar(70), #KeepNumeric as bit = 0)
returns nvarchar(25)
/*
Metaphone Algorithm
Created by Lawrence Philips.
Metaphone presented in article in "Computer Language" December 1990 issue.
*********** BEGIN METAPHONE RULES ***********
Lawrence Philips' RULES follow:
The 16 consonant sounds:
|--- ZERO represents "th"
|
B X S K J T F H L M N P R 0 W Y
Drop vowels
Exceptions:
Beginning of word: "ae-", "gn", "kn-", "pn-", "wr-" ----> drop first letter
Beginning of word: "wh-" ----> change to "w"
Beginning of word: "x" ----> change to "s"
Beginning of word: vowel or "H" + vowel ----> Keep it
Transformations:
B ----> B unless at the end of word after "m", as in "dumb", "McComb"
C ----> X (sh) if "-cia-" or "-ch-"
S if "-ci-", "-ce-", or "-cy-"
SILENT if "-sci-", "-sce-", or "-scy-"
K otherwise
K "-sch-"
D ----> J if in "-dge-", "-dgy-", or "-dgi-"
T otherwise
F ----> F
G ----> SILENT if "-gh-" and not at end or before a vowel
"-gn" or "-gned"
"-dge-" etc., as in above rule
J if "gi", "ge", "gy" if not double "gg"
K otherwise
H ----> SILENT if after vowel and no vowel follows
or "-ch-", "-sh-", "-ph-", "-th-", "-gh-"
H otherwise
J ----> J
K ----> SILENT if after "c"
K otherwise
L ----> L
M ----> M
N ----> N
P ----> F if before "h"
P otherwise
Q ----> K
R ----> R
S ----> X (sh) if "sh" or "-sio-" or "-sia-"
S otherwise
T ----> X (sh) if "-tia-" or "-tio-"
0 (th) if "th"
SILENT if "-tch-"
T otherwise
V ----> F
W ----> SILENT if not followed by a vowel
W if followed by a vowel
X ----> KS
Y ----> SILENT if not followed by a vowel
Y if followed by a vowel
Z ----> S
*/
as
begin
declare #Result varchar(25)
,#str3 char(3)
,#str2 char(2)
,#str1 char(1)
,#strp char(1)
,#strLen tinyint
,#cnt tinyint
set #strLen = len(#str)
set #cnt = 0
set #Result = ''
-- Preserve first 5 numeric values when required
if #KeepNumeric = 1
begin
set #Result = case when isnumeric(substring(#str,1,1)) = 1
then case when isnumeric(substring(#str,2,1)) = 1
then case when isnumeric(substring(#str,3,1)) = 1
then case when isnumeric(substring(#str,4,1)) = 1
then case when isnumeric(substring(#str,5,1)) = 1
then left(#str,5)
else left(#str,4)
end
else left(#str,3)
end
else left(#str,2)
end
else left(#str,1)
end
else ''
end
set #str = right(#str,len(#str)-len(#Result))
end
--Process beginning exceptions
set #str2 = left(#str,2)
if #str2 = 'wh'
begin
set #str = 'w' + right(#str , #strLen - 2)
set #strLen = #strLen - 1
end
else
if #str2 in('ae', 'gn', 'kn', 'pn', 'wr')
begin
set #str = right(#str , #strLen - 1)
set #strLen = #strLen - 1
end
set #str1 = left(#str,1)
if #str1 = 'x'
set #str = 's' + right(#str , #strLen - 1)
else
if #str1 in ('a','e','i','o','u')
begin
set #str = right(#str, #strLen - 1)
set #strLen = #strLen - 1
set #Result = #Result + #str1
end
while #cnt <= #strLen
begin
set #cnt = #cnt + 1
set #str1 = substring(#str,#cnt,1)
set #strp = case when #cnt <> 0
then substring(#str,(#cnt-1),1)
else ' '
end
-- Check if the current character is the same as the previous character.
-- If we are keeping numbers, only compare non-numeric characters.
if case when #KeepNumeric = 1 and #strp = #str1 and isnumeric(#str1) = 0 then 1
when #KeepNumeric = 0 and #strp = #str1 then 1
else 0
end = 1
continue -- Skip this loop
set #str2 = substring(#str,#cnt,2)
set #Result = case when #KeepNumeric = 1 and isnumeric(#str1) = 1
then #Result + #str1
when #str1 in('f','j','l','m','n','r')
then #Result + #str1
when #str1 = 'q'
then #Result + 'k'
when #str1 = 'v'
then #Result + 'f'
when #str1 = 'x'
then #Result + 'ks'
when #str1 = 'z'
then #Result + 's'
when #str1 = 'b'
then case when #cnt = #strLen
then case when substring(#str,(#cnt - 1),1) <> 'm'
then #Result + 'b'
else #Result
end
else #Result + 'b'
end
when #str1 = 'c'
then case when #str2 = 'ch' or substring(#str,#cnt,3) = 'cia'
then #Result + 'x'
else case when #str2 in('ci','ce','cy') and #strp <> 's'
then #Result + 's'
else #Result + 'k'
end
end
when #str1 = 'd'
then case when substring(#str,#cnt,3) in ('dge','dgy','dgi')
then #Result + 'j'
else #Result + 't'
end
when #str1 = 'g'
then case when substring(#str,(#cnt - 1),3) not in ('dge','dgy','dgi','dha','dhe','dhi','dho','dhu')
then case when #str2 in('gi', 'ge','gy')
then #Result + 'j'
else case when #str2 <> 'gn' or (#str2 <> 'gh' and #cnt+1 <> #strLen)
then #Result + 'k'
else #Result
end
end
else #Result
end
when #str1 = 'h'
then case when #strp not in ('a','e','i','o','u') and #str2 not in ('ha','he','hi','ho','hu')
then case when #strp not in ('c','s','p','t','g')
then #Result + 'h'
else #Result
end
else #Result
end
when #str1 = 'k'
then case when #strp <> 'c'
then #Result + 'k'
else #Result
end
when #str1 = 'p'
then case when #str2 = 'ph'
then #Result + 'f'
else #Result + 'p'
end
when #str1 = 's'
then case when substring(#str,#cnt,3) in ('sia','sio') or #str2 = 'sh'
then #Result + 'x'
else #Result + 's'
end
when #str1 = 't'
then case when substring(#str,#cnt,3) in ('tia','tio')
then #Result + 'x'
else case when #str2 = 'th'
then #Result + '0'
else case when substring(#str,#cnt,3) <> 'tch'
then #Result + 't'
else #Result
end
end
end
when #str1 = 'w'
then case when #str2 not in('wa','we','wi','wo','wu')
then #Result + 'w'
else #Result
end
when #str1 = 'y'
then case when #str2 not in('ya','ye','yi','yo','yu')
then #Result + 'y'
else #Result
end
else #Result
end
end
return #Result
end
You want to use something like Levenshtein Distance or another string comparison algorithm. You may want to take a look at this project on Codeplex.
http://fuzzystring.codeplex.com/
Are you using Access? If so, consider the '*' character, without the quotes. If you're using SQL Server, use the '%' character. However, this really isn't fuzzy logic, it's really the Like operator. If you really need fuzzy logic, export your data-set to Excel and load the AddIn from the URL below.
https://www.microsoft.com/en-us/download/details.aspx?id=15011
Read the instructions very carefully. It definitely works, and it works great, but you need to follow the instructions, and it's not completely intuitive. The first time I tried it, I didn't follow the instructions, and I wasted a lot of time trying to get it to work. Eventually I figured it out, and it worked great!!
I found success implementing a function I found here on Stack Overflow that would find the percentage of strings that match. You can then adjust tolerance till you get an appropriate amount of matches/mismatches. The function implementation will be listed below, but the gist is including something like this in your query.
DECLARE #tolerance DEC(18, 2) = 50;
WHERE dbo.GetPercentageOfTwoStringMatching(first_table.name, second_table.name) > #tolerance
Credit for the following percent matching function goes to Dragos Durlut, Dec 15 '11.
The credit for the LEVENSHTEIN function was included in the code by Dragos Durlut.
T-SQL Get percentage of character match of 2 strings
CREATE FUNCTION [dbo].[GetPercentageOfTwoStringMatching]
(
#string1 NVARCHAR(100)
,#string2 NVARCHAR(100)
)
RETURNS INT
AS
BEGIN
DECLARE #levenShteinNumber INT
DECLARE #string1Length INT = LEN(#string1)
, #string2Length INT = LEN(#string2)
DECLARE #maxLengthNumber INT = CASE WHEN #string1Length > #string2Length THEN #string1Length ELSE #string2Length END
SELECT #levenShteinNumber = [dbo].[LEVENSHTEIN] ( #string1 ,#string2)
DECLARE #percentageOfBadCharacters INT = #levenShteinNumber * 100 / #maxLengthNumber
DECLARE #percentageOfGoodCharacters INT = 100 - #percentageOfBadCharacters
-- Return the result of the function
RETURN #percentageOfGoodCharacters
END
-- =============================================
-- Create date: 2011.12.14
-- Description: http://blog.sendreallybigfiles.com/2009/06/improved-t-sql-levenshtein-distance.html
-- =============================================
CREATE FUNCTION [dbo].[LEVENSHTEIN](#left VARCHAR(100),
#right VARCHAR(100))
returns INT
AS
BEGIN
DECLARE #difference INT,
#lenRight INT,
#lenLeft INT,
#leftIndex INT,
#rightIndex INT,
#left_char CHAR(1),
#right_char CHAR(1),
#compareLength INT
SET #lenLeft = LEN(#left)
SET #lenRight = LEN(#right)
SET #difference = 0
IF #lenLeft = 0
BEGIN
SET #difference = #lenRight
GOTO done
END
IF #lenRight = 0
BEGIN
SET #difference = #lenLeft
GOTO done
END
GOTO comparison
COMPARISON:
IF ( #lenLeft >= #lenRight )
SET #compareLength = #lenLeft
ELSE
SET #compareLength = #lenRight
SET #rightIndex = 1
SET #leftIndex = 1
WHILE #leftIndex <= #compareLength
BEGIN
SET #left_char = substring(#left, #leftIndex, 1)
SET #right_char = substring(#right, #rightIndex, 1)
IF #left_char <> #right_char
BEGIN -- Would an insertion make them re-align?
IF( #left_char = substring(#right, #rightIndex + 1, 1) )
SET #rightIndex = #rightIndex + 1
-- Would an deletion make them re-align?
ELSE IF( substring(#left, #leftIndex + 1, 1) = #right_char )
SET #leftIndex = #leftIndex + 1
SET #difference = #difference + 1
END
SET #leftIndex = #leftIndex + 1
SET #rightIndex = #rightIndex + 1
END
GOTO done
DONE:
RETURN #difference
END
Note: If you need to compare two or more fields (which I don't think you do) you can add another call to the function in the WHERE clause with a minimum tolerance. I also found success averaging the percentMatching and comparing it against a tolerance.
DECLARE #tolerance DEC(18, 2) = 25;
--could have multiple different tolerances for each field (weighting some fields as more important to be matching)
DECLARE #avg_tolerance DEC(18, 2) = 50;
WHERE AND dbo.GetPercentageOfTwoStringMatching(first_table.name, second_table.name) > #tolerance
AND dbo.GetPercentageOfTwoStringMatching(first_table.address, second_table.address) > #tolerance
AND (dbo.GetPercentageOfTwoStringMatching(first_table.name, second_table.name)
+ dbo.GetPercentageOfTwoStringMatching(first_table.address, second_table.address)
) / 2 > #avg_tolerance
The benefit of this solution is the tolerance variables can be specific per field (weighting the importance of certain fields matching) and the average can insure general matching across all fields.
Firstly, I suggest, you make sure that you can't match on any other attribute and company names are all you have(because fuzzy matching is bound to give you some false positives). If you want to go ahead with fuzzy matching you could use the following steps:
Remove all stop words from the text. For example : Co, Inc etc.
If your database is very large, make use of an indexing method such as blocking or sorted neighbourhood indexing.
Finally compute the fuzzy score using the Levenshtein distance. You could use the token_set_ratio or partial_ratio functions in Fuzzywuzzy.
Also, I found the following video which aims to solve the same problem: https://www.youtube.com/watch?v=NRAqIjXaZvw
The Nanonets blog also contains several resources on the subject that could potentially be helpful.

CASE statement plus modulo (%)

AIM: simple program; when my variable is divided into 3 it returns the word'hip', when it is divided into 5 it returns 'hop' and when it is divided into 3 & 5 at the same time it returns both words.
DECLARE #Zmienna AS INT
SET #Zmienna = 0
WHILE #Zmienna < 999
BEGIN
PRINT #Zmienna +
CASE
WHEN #Zmienna/3=% THEN ' hip'
WHEN #Zmienna/5=% THEN ' hop'
END
SET #Zmienna = #Zmienna + 1
END
Error
ERROR: Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'THEN'.
Msg 102, Level 15, State 1, Line 12
Incorrect syntax near 'END'.
Any idea?
I would use the remainder of the modulo (like you tried) and concatenate the two case statements (else you will never get hiphop if both conditions are true). Also you need the else '' since otherwise you can get null values:
CASE
WHEN #Zmienna % 3 = 0
THEN ' hip'
ELSE ''
END
+
CASE
WHEN #Zmienna % 5 = 0
THEN ' hop'
ELSE ''
END
Hint: if you want a space or some other text if both conditions are true, you have to use an and in the case statement:
CASE
WHEN #Zmienna % 3 = 0 and #Zmienna % 5 = 0
THEN ' hip hop'
WHEN #Zmienna % 3 = 0
THEN ' hip'
WHEN #Zmienna % 5 = 0
THEN ' hop'
END
try this:
DECLARE #Zmienna AS INT
SET #Zmienna = 0
WHILE #Zmienna < 999
BEGIN
PRINT CAST(#Zmienna as varchar) +
CASE
when (#Zmienna%3=0 AND #Zmienna%5=0) THEN ' hip hop'
WHEN #Zmienna%3=0 THEN ' hip'
WHEN #Zmienna%5=0 THEN ' hop'
END
SET #Zmienna = #Zmienna + 1
END
Try this, it will give you only 1 dataset instead of 999 for better performance and readability.
Print can only handle 1 value, instead you can SELECT all the rows in 1 go and display the values in different columns instead of concatinating them:
;WITH CTE as
(
SELECT 0 Zmienna
UNION ALL
SELECT Zmienna + 1
FROM CTE
WHERE Zmienna < 998 -- i wonder why you don't want to include 999
)
SELECT
Zmienna,
CASE
WHEN Zmienna % 15 = 0 THEN 'hiphop'
WHEN Zmienna % 3 = 0 THEN 'hip'
WHEN Zmienna % 5 = 0 THEN 'hop'
END as Hippityhop
FROM CTE
OPTION (maxrecursion 0)
you can do it with IF statement
DECLARE #Zmienna AS INT
SET #Zmienna = 0
WHILE #Zmienna < 999
BEGIN
if (#Zmienna % 3) = 0 and (#Zmienna % 5) = 0
BEGIN
PRINT convert(varchar(10),#Zmienna)+ ' hip'+' '+'hop'
END
ELSE
BEGIN
if (#Zmienna % 3) = 0
BEGIN
PRINT convert(varchar(10),#Zmienna)+' hip'
END
if (#Zmienna % 5) = 0
BEGIN
select convert(varchar(10),#Zmienna)+' hop'
END
END
SET #Zmienna = #Zmienna + 1
END
It seems like the easiest answer:
DECLARE #Zmienna AS INT
SET #Zmienna = 0
WHILE #Zmienna < 999
BEGIN
PRINT CAST(#Zmienna as varchar) +
CASE
when (#Zmienna%3=0 AND #Zmienna%5=0) THEN ' hip hop'
WHEN #Zmienna%3=0 THEN ' hip'
WHEN #Zmienna%5=0 THEN ' hop'
ELSE ''
END
SET #Zmienna = #Zmienna + 1
END

SQL Sum of each character in a number

I want to sum each character of an integer in SQL
For instance. I have 16273481 as INT
But now (Without to complicated methods) sum
1 + 6 + 2 + 7 + 3 + 4 + 8 + 1 = 32
DECLARE #someInt INT = 16273481
-- you could put this all into a function
-- and then it would be reusable...
--
-- like... SELECT SumOfIndividualIntegers(16273481)
DECLARE #count INT = LEN(#someInt),
#counter INT = 1
DECLARE #Sum INT = 0
WHILE #counter <= #count
BEGIN
SELECT #sum += CAST(SUBSTRING(CAST(#someInt AS VARCHAR), #counter, 1) AS int)
SELECT #counter += 1
END
SELECT #sum --32
-- and then you would RETURN #sum instead
Would using the remainder operator be suitable for your situation with a loop?
Pseuodo code:
x = 16273481;
sum = 0;
Loop:
sum = sum + (x % 10);
x = (x / 10);
Something along those lines?

SQL proc to calculate check digit for 7 and 12 digit upc

I just had to scour the internet for this code yet again so I figured I would put it here so I can find it a little faster the next time and hopefully you found it a little faster too :)
Check this.
The code below can check digit in all GTIN's (EAN8, EAN13, EAN14, UPC/A, UPC/E)
CREATE FUNCTION [dbo].[check_digit]
(
#GTIN VARCHAR(14)
)
RETURNS TINYINT
AS
BEGIN
DECLARE #Index TINYINT,
#Multiplier TINYINT,
#Sum TINYINT,
#checksum TINYINT,
#result TINYINT,
#GTIN_strip VARCHAR(13)
SELECT #GTIN_strip = SUBSTRING(#GTIN,1,LEN(#GTIN)-1);
SELECT #Index = LEN(#GTIN_strip),
#Multiplier = 3,
#Sum = 0
WHILE #Index > 0
SELECT #Sum = #Sum + #Multiplier * CAST(SUBSTRING(#GTIN_strip, #Index, 1) AS TINYINT),
#Multiplier = 4 - #Multiplier,
#Index = #Index - 1
SELECT #checksum = CASE #Sum % 10
WHEN 0 THEN '0'
ELSE CAST(10 - #Sum % 10 AS CHAR(1))
END
IF (SUBSTRING(#GTIN,LEN(#GTIN),1) = #checksum)
RETURN 1; /*true*/
RETURN 0; /*false*/
END
CREATE FUNCTION [dbo].[fn_EAN13CheckDigit] (#Barcode nvarchar(12))
RETURNS nvarchar(13)
AS
BEGIN
DECLARE #SUM int , #COUNTER int, #RETURN varchar(13), #Val1 int, #Val2 int
SET #COUNTER = 1 SET #SUM = 0
WHILE #Counter < 13
BEGIN
SET #VAL1 = SUBSTRING(#Barcode,#counter,1) * 1
SET #VAL2 = SUBSTRING(#Barcode,#counter + 1,1) * 3
SET #SUM = #VAL1 + #SUM
SET #SUM = #VAL2 + #SUM
SET #Counter = #Counter + 2;
END
SET #SUM=(10-(#SUM%10))%10
SET #Return = #BARCODE + CONVERT(varchar,((#SUM)))
RETURN #Return
END
And here's a MySQL implementation as well.
Note it requires a 13 digit UPC/EAN13 as input, so you should LPAD your input as required.
drop function if exists barcodify;
delimiter //
at the last digit.
create function barcodify(upc varchar(15))
returns varchar(15)
sql security invoker
begin
declare i, r, odd, even,result int;
set odd=0;
set even =0;
set i = length(upc);
while i > 0 do
set r = substring(upc, i, 1) ;
if(i % 2 >0) then set odd = odd + r;
else set even = even + r;
end if;
set i = i - 1;
end while;
set result = (3*odd)+even;
if result % 10 =0 then return 0;
else return (10 - (result %10));
end if;
end //
delimiter ;
Here's my implementation of a function in Oracle.
CREATE OR REPLACE FUNCTION GenerateCheckDigit(pUPC IN VARCHAR2) RETURN INT
IS
vCheckDigit INT;
BEGIN
WITH barcodeData AS
(
SELECT pUPC barcode FROM dual
)
SELECT
10-REPLACE(MOD(SUM(SUBSTR(barcode, -LEVEL, 1) * DECODE(MOD(LEVEL-1, 2), 1, 1, 3)),10),0,10) INTO vCheckDigit
FROM barcodeData
CONNECT BY LEVEL <= LENGTH(barcode);
RETURN vCheckDigit;
END;
To calculate the last digit.
https://segovoni.medium.com/sql-server-function-to-calculate-gs1-barcode-check-digit-d55b478ff645
CREATE FUNCTION dbo.udf_GetGS1EAN13CheckDigit
(
#ACode AS VARCHAR(12)
)
RETURNS SMALLINT
AS BEGIN
/*
Author: Sergio Govoni
Notes: Calculate the check-digit of a GS1 EAN13 code
Version: 1.0
*/
DECLARE
#tmpCode AS VARCHAR(12)
,#tmpMulSup AS VARCHAR(8000)
,#tmp AS VARCHAR(8000)
,#i AS INT
,#j AS INT
,#z AS INT
,#SumDEven AS INT
,#SumDOdd AS INT
,#List AS VARCHAR(8000)
,#tmpList AS VARCHAR(8000)
,#CheckSum AS SMALLINT
SET #SumDEven = 0
SET #SumDOdd = 0
SET #List = ''
SET #tmpList = ''
SET #tmp = ''
SET #tmpCode = #ACode
/* 0. List builder */
SET #j = LEN(#tmpCode) + 1
SET #i = 1
WHILE (#i <= LEN(#tmpCode)) BEGIN SET #List = #List + '|' + LTRIM(RTRIM(STR(#j))) + ';' + SUBSTRING(#tmpCode, #i, 1) SET #j = (#j - 1) SET #i = (#i + 1) END /* 1. Add up the digits in even position */ SET #i = 1 SET #tmpList = #List WHILE (CHARINDEX('|', #tmpList) > 0)
BEGIN
SET #j = CHARINDEX('|', #tmpList)
SET #z = CHARINDEX(';', #tmpList)
IF (CAST(SUBSTRING(#tmpList, (#j + 1), (#z - (#j + 1))) AS INTEGER) % 2) = 0
BEGIN
SET #SumDEven = #SumDEven + CAST(SUBSTRING(#tmpList, (#z + 1), 1) AS INTEGER)
END
SET #tmpList = SUBSTRING(#tmpList, (#z + 2), LEN(#tmpList))
END
/* 2. Multiply the result of the previous step (the first step) to 3 (three) */
SET #SumDEven = (#SumDEven * 3)
/* 3. Add up the digits in the odd positions */
SET #i = 1
SET #tmpList = #List
WHILE (CHARINDEX('|', #tmpList) > 0)
BEGIN
SET #j = CHARINDEX('|', #tmpList)
SET #z = CHARINDEX(';', #tmpList)
IF (CAST(SUBSTRING(#tmpList, (#j + 1), (#z - (#j + 1))) AS INTEGER) % 2) <> 0
BEGIN
SET #SumDOdd = #SumDOdd + CAST(SUBSTRING(#tmpList, (#z + 1), 1) AS INTEGER)
END
SET #tmpList = SUBSTRING(#tmpList, (#z + 2), LEN(#tmpList))
END
/* 4. Add up the results obtained in steps two and three */
SET #CheckSum = (#SumDEven + #SumDOdd)
/* 5. Subtract the upper multiple of 10 from the result obtained in step four */
IF ((#CheckSum % 10) = 0)
BEGIN
/* If the result of the four step is a multiple of Ten (10), like
Twenty, Thirty, Forty and so on,
the check-digit will be equal to zero, otherwise the check-digit will be
the result of the fifth step
*/
SET #CheckSum = 0
END
ELSE BEGIN
SET #tmpMulSup = LTRIM(RTRIM(STR(#CheckSum)))
SET #i = 0
WHILE #i <= (LEN(#tmpMulSup) - 1)
BEGIN
SET #tmp = #tmp + SUBSTRING(#tmpMulSup, #i, 1)
IF (#i = LEN(#tmpMulSup) - 1)
BEGIN
SET #tmp = LTRIM(RTRIM(STR(CAST(#tmp AS INTEGER) + 1)))
SET #tmp = #tmp + '0'
END
SET #i = (#i + 1)
END
SET #CheckSum = CAST(#tmp AS INTEGER) - #CheckSum
END
RETURN #CheckSum
END;
CREATE FUNCTION sfn_ean_chkdigit(#barcode varchar(20))
RETURNS CHAR(1)
AS
BEGIN
DECLARE #chk_digit int, #chk int
DECLARE #num TABLE (num int)
IF LEN(#barcode) NOT IN (7, 12) RETURN NULL
INSERT INTO #num
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
SELECT #chk_digit = SUM(CONVERT(int, SUBSTRING(#barcode, LEN(#barcode) - num + 1, 1)) * CASE WHEN num % 2 = 1 THEN 3 ELSE 1 END)
FROM #num
WHERE num <= LEN(#barcode)
SELECT #chk_digit = (10 - (#chk_digit % 10)) % 10
RETURN CHAR(ASCII('0') + #chk_digit)
END
Num unico script
select lpad('123456789012',12,'0') || ( (r+1)*10 - soma )%10
from (select sum(v::int * case when i%2=0 then 3 else 1 end) soma,
round(sum(v::int * case when i%2=0 then 3 else 1 end)/10.0) r
from unnest(string_to_array(lpad('123456789012',12,'0'),null)) with ordinality d(v,i)
) a