Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 ISBN algorithms

Author  Topic 

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-24 : 08:21:42
Here is how you get the check digit for ISBN.
CREATE FUNCTION dbo.fnGetISBN
(
@ISBN VARCHAR(11)
)
RETURNS VARCHAR(13)
AS
BEGIN
DECLARE @Index TINYINT,
@Weight TINYINT,
@Sum SMALLINT

SELECT @Index = LEN(@ISBN),
@Weight = 2,
@Sum = 0

WHILE @Index > 0
BEGIN
IF SUBSTRING(@ISBN, @Index, 1) <> '-'
SELECT @Sum = @Sum + @Weight * CAST(SUBSTRING(@ISBN, @Index, 1) AS TINYINT),
@Weight = @Weight + 1

SET @Index = @Index - 1
END

RETURN CASE @Sum % 11
WHEN 1 THEN @ISBN + '-X'
ELSE @ISBN + '-' + CONVERT(CHAR(1), 11 - (@Sum % 11))
END
END


E 12°55'05.25"
N 56°04'39.16"

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-08-24 : 08:27:10
too much time again?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-24 : 08:34:04
quote:
Originally posted by spirit1

too much time again?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp


No doubt

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -