Advertisement
Guest User

ISBNNumber_Unfinished

a guest
May 20th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.39 KB | None | 0 0
  1. -- ISBN number numerically valid?
  2. --  The ISBN-10 check digit, which is the last digit of the ten-digit ISBN, must range from 0 - 10 (X in place of 10).
  3. --  And must be such that the sum of all the 10 digits, each multiplied by its (integer) weightm descending from 10 to 1, is a multiple of 11.
  4.  
  5. /* Example 1:
  6.     0-306-40615-2
  7.     = (0 * 10) + (3 * 9) + (0 * 8) + (6 * 7) + (4 * 6) + (0 * 5) + (6 * 4) + (1 * 3) + (5 * 2) + (2 * 1)
  8.     = 0 + 27 + 0 + 42 + 24 + 0 + 24 + 3 + 10 + 2
  9.     = 132
  10.     = 11 * 12
  11. */
  12.  
  13. -- UNFINISHED:
  14. --  Need to account for ending in X
  15. --  Need to account for non numeric characters
  16.  
  17. -- Create and populate a test table.
  18. CREATE TABLE #ISBNTest
  19. (
  20.     ISBNTestID INT IDENTITY(1, 1) CONSTRAINT [PK_ISBNTest_ISBNTestID] PRIMARY KEY CLUSTERED,
  21.     ISBNNumberInsert VARCHAR(13) NOT NULL,
  22.     ISBNNumber  AS (REPLACE(ISBNNumberInsert, '-', '')) -- We can remove hyphens straight away this way (computed column)
  23. );
  24. GO
  25.  
  26. INSERT INTO #ISBNTest (ISBNNumberInsert) VALUES ('0-306-40615-2');
  27.  
  28. /* -- If we don't want to use computed column then can do the following
  29. CREATE TABLE #ISBNTest
  30. (
  31.     ISBNTestID int identity(1, 1) CONSTRAINT [PK_ISBNTest_ISBNTestID] PRIMARY KEY CLUSTERED,
  32.     ISBNNumberInsert varchar(13) NOT NULL,
  33.     ISBNNumber  varchar(13) NOT NULL
  34. );
  35. GO
  36.  
  37. INSERT INTO #ISBNTest (ISBNNumberInsert) VALUES ('0-306-40615-2');
  38.  
  39. UPDATE #ISBNTest SET ISBNNumber = REPLACE(ISBNNumberInsert, '-'. '');
  40.  
  41. */
  42.  
  43. SELECT  *
  44. FROM    #ISBNTest;
  45.  
  46. -- Tally table method with SUM() over.
  47. WITH    L0 AS(SELECT 1 AS c UNION ALL SELECT 1),
  48.         L1 AS(SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B),
  49.         L2 AS(SELECT 1 AS c FROM L1 AS A CROSS JOIN L1 AS B),
  50.         TallyTable (n) AS
  51.         (
  52.         SELECT  TOP(10) --  We can hard code this as we know ISBN is only 10 chars long
  53.                 ROW_NUMBER() OVER (ORDER BY (SELECT NULL) ASC)
  54.         FROM    L2
  55.         )
  56. SELECT  RetVal =
  57.                 CASE
  58.                     WHEN SUM(S.SumOfWeights) OVER (PARTITION BY S.ISBNTestID) % 11 = 0 THEN 'True'
  59.                     ELSE 'False'
  60.                 END,
  61.         SumOverOfWeights = SUM(S.SumOfWeights) OVER (PARTITION BY S.ISBNTestID),
  62.         S.*
  63. FROM    (
  64. SELECT  I.ISBNTestID,
  65.         I.ISBNNumberInsert,
  66.         I.ISBNNumber,
  67.         X.EachNumber,
  68.         TT.n,
  69.         SumOfWeights = X.EachNumber * TT.n
  70. FROM    #ISBNTest AS [I]
  71. CROSS
  72. APPLY   (SELECT TOP (10) TallyTable.n FROM TallyTable ORDER BY TallyTable.n DESC) AS [TT]
  73. CROSS
  74. APPLY   (SELECT SUBSTRING(I.ISBNNumber, TT.n, 1)) AS [X] (EachNumber)
  75.         ) AS [S];
  76. GO
  77.                
  78. -- Clean up
  79. DROP TABLE #ISBNTest;
  80. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement