Guest User

Untitled

a guest
Jan 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. -- The function is used to convert employee ID into a (random)
  2. -- character. To achieve randomness a seed should be generated by a caller.
  3. -- Note: the number of IDs to be encoded cannot be greater than 62, otherwise
  4. -- the function will return conflicting characters. To increase the number of handled
  5. -- IDs extended the @chars variable
  6. -- Usage:
  7. -- select dbo.fn_id2alpha(1223, 42,
  8.  
  9.  
  10. create function [dbo].[fn_id2alpha](@id int, @seed int, @n int)
  11. -- @id - ID to be converted into a random char
  12. -- @seed - random number, use select @seed = floor(rand()*100) to init
  13. -- @n - the total number of IDs to be randomly encoded, should be no greater than 62
  14. returns nvarchar(1)
  15. as
  16. begin
  17. declare @chars nvarchar(255)
  18. select @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  19. if @n > len(@chars) select @n = len(@chars)
  20. select @id = (@id + @seed)%@n + 1
  21. return substring(@chars, @id, 1)
  22. end
Add Comment
Please, Sign In to add comment