The customer data was staged in a SQL database and the goal was to create a query that will assign these customer IDs based on the customer name. So, here is the solution:
AutoCustomerID.sql
-- Created by Mariano Gomez, MVP
SELECT
-- evaluates the 9 first characters of the customer name and removes any blanks
-- other characters can be removed) in between those first 9 characters for a
-- total of 8, adds an extra zero if needed to complete 9 characters
CASE LEN(UPPER(REPLACE(SUBSTRING(CUSTNAME, 1, 9), ' ', '')))
WHEN 8 THEN UPPER(REPLACE(SUBSTRING(CUSTNAME, 1, 9), ' ', '')) + '0'
ELSE UPPER(REPLACE(SUBSTRING(CUSTNAME, 1, 9), ' ', '')) END +
-- accounts for the rest of the string, uses the rank function to do the numbering,
-- partitioning by customer name. Just in case there is more than one customer
-- with the same starting 9 characters, rank() will number them sequentially
SUBSTRING('000', 1, 3 - LEN(CONVERT(CHAR(3), RANK() OVER(PARTITION BY REPLACE
(SUBSTRING(CUSTNAME, 1, 9), ' ', '') ORDER BY CUSTNAME))))
+ CONVERT(CHAR(3), RANK() OVER(PARTITION BY REPLACE(SUBSTRING(CUSTNAME, 1,
9), ' ', '') ORDER BY CUSTNAME))
FROM RM00101
ORDER BY CUSTNAME
Run this against the TWO database to see the results.
If you ever have the need to create customer IDs based on the customer name this should give you a starting point.
Until next post!
MG.-
Mariano Gomez, MVP
Maximum Global Business, LLC
http://www.maximumglobalbusiness.com/
0 comments:
Post a Comment