I ran into this issue when attempting to use System->Import with a CSV file when importing a large number of products. I generated the CSV with a MySQL query and, using this UDF, generated my own URL Key for each product rather than allowing Magento to generate it.
So, my SELECT statement has the following for generating the URL Key:
CONCAT(LOWER(REPLACE(remove_non_alphanum_char_f(TRIM(`Description`)), ' ', '-')), '-', `Internal ID`,FLOOR(10000 + RAND() * 89999)) AS url_key
Working from inside-out:
- Remove trailing spaces from my
Description
field using MySQL's TRIM() function
- Using the UDF ("remove_non_alphanum_char_f()"), remove non-alphanumeric characters by replacing them with hyphens
- Lowercase the string using MySQL's LOWER() function
- Using MySQL's CONCAT() function, concatenate the resulting string with my product's Internal ID AND a randomly-generated integer. You may not wish to include your own product ID, BUT THE RANDOMLY-GENERATED INTEGER IS IMPORTANT! It's what'll make your URL Key for this particular product unique from others that might have a similar product name.
There's probably a more-elegant way to do this but it got me over the hump on this recurring URL Key issu. Hope this helps!