Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

UPC Barcode Checksum sql function

193 views
Skip to first unread message

Mark Goldin

unread,
Oct 23, 2009, 2:48:13 PM10/23/09
to
If anyone has SQL code to generate an UPC barcode do you mind to share it?

TIA

Jeroen Mostert

unread,
Oct 23, 2009, 3:12:38 PM10/23/09
to
Mark Goldin wrote:
> If anyone has SQL code to generate an UPC barcode do you mind to share it?
>
I hope you do mean generating the *check digit*, as implied in the subject.
Generating the actual barcode from SQL is a lot more challenging.

Assuming that "@barcode" is a string containing the digits of a UPC-A
barcode, this should do:

SELECT @checkdigit = (
10 - (
(
CONVERT(INT, SUBSTRING(@barcode, 1, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 3, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 5, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 7, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 9, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 11, 1))
) * 3 + (
CONVERT(INT, SUBSTRING(@barcode, 2, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 4, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 6, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 8, 1)) +
CONVERT(INT, SUBSTRING(@barcode, 10, 1))
)
) % 10
) % 10;

--
J.

Mark Goldin

unread,
Oct 23, 2009, 3:25:51 PM10/23/09
to
Sorry for confusion. I do need to generate a complete UPC barcode for a
given string.


"Jeroen Mostert" <jmos...@xs4all.nl> wrote in message
news:4ae20027$0$83243$e4fe...@news.xs4all.nl...

Jeroen Mostert

unread,
Oct 23, 2009, 3:49:51 PM10/23/09
to
Mark Goldin wrote:
> Sorry for confusion. I do need to generate a complete UPC barcode for a
> given string.
>
It's still not clear what you mean.

If you mean producing a barcode *image*, that's not something you want to do
in SQL even if you can (in theory). Use client code and one of the many
available software packages for that.

If you mean producing barcode digits from a string, there is no single
uniform way to do that. UPC is a digit-only barcode, you cannot use it to
represent strings unless you already have a specific mapping between strings
and digits in mind. There are barcodes that can represent strings (like Code
128) but those do not use digits as such, just different barcode widths.

--
J.

Mark Goldin

unread,
Oct 23, 2009, 3:58:24 PM10/23/09
to
No, not an image.
I have a string (yes, digits only) that has to be converted into an UPC
number in order to
be printed out as a barcode of course using barcode font.


"Jeroen Mostert" <jmos...@xs4all.nl> wrote in message

news:4ae208e0$0$83236$e4fe...@news.xs4all.nl...

Jeroen Mostert

unread,
Oct 23, 2009, 4:14:04 PM10/23/09
to
Mark Goldin wrote:
> I have a string (yes, digits only) that has to be converted into an UPC
> number in order to
> be printed out as a barcode of course using barcode font.
>
A UPC barcode *is* a string of digits (when not represented as actual bars).
It's unclear what conversion you need. If you need to convert it to specific
characters to properly print with your font, consult the documentation of
your font to find out what characters they should be.

--
J.

0 new messages