TSQL Random String Generator

http://xsqlsoftware.blogspot.com/2008/12/t-sql-random-string-generator.html

언어 : sql
  1. SET ANSI_NULLS ON
  2. SET QUOTED_IDENTIFIER ON
  3. go
  4.  
  5. CREATE PROCEDURE [dbo].[usp_GenerateRandomString]
  6.   @sLength tinyint = 10,
  7.   @randomString varchar(50) OUTPUT
  8. AS
  9. BEGIN
  10.   SET NOCOUNT ON
  11.   DECLARE @counter tinyint
  12.   DECLARE @nextChar char(1)
  13.   SET @counter = 1
  14.   SET @randomString = ''
  15.  
  16.   WHILE @counter <= @sLength
  17.     BEGIN
  18.       SELECT @nextChar = CHAR(ROUND(RAND() * 93 + 33, 0))
  19.       IF ASCII(@nextChar) NOT IN (34, 39, 40, 41, 44, 46, 96, 58, 59)
  20.         BEGIN
  21.           SELECT @randomString = @randomString + @nextChar
  22.           SET @counter = @counter + 1
  23.         END
  24.     END
  25. END


http://codebetter.com/blogs/raymond.lewallen/archive/2005/01/13/43938.aspx

언어 : sql
  1. declare @string varchar(10)
  2. declare @string2 varchar(10)
  3. declare @choices varchar(100)
  4. declare @count int
  5.  
  6. SET @choices = ''
  7.  
  8. -- load up numbers 0 - 9
  9. SET @count = 48
  10. while @count <=57
  11. begin
  12.     SET @choices = @choices + Cast(CHAR(@count) AS char(1))
  13.     SET @count = @count + 1
  14. end
  15.  
  16. -- load up uppercase letters A - Z
  17. SET @count = 65
  18. while @count <=90
  19. begin
  20.     SET @choices = @choices + Cast(CHAR(@count) AS char(1))
  21.     SET @count = @count + 1
  22. end
  23.  
  24. -- load up lowercase letters a - z
  25. SET @count = 97
  26. while @count <=122
  27. begin
  28.     SET @choices = @choices + Cast(CHAR(@count) AS char(1))
  29.     SET @count = @count + 1
  30. end
  31.  
  32. declare @whatever varchar(100)
  33. SET @whatever = 'JackAndJillWentUpTheHillToFetchAPailOfH20'
  34. SET @whatever = @whatever + 'PittsburghWinsTheSuperBowl'
  35.  
  36. SET @count = 0
  37. SET @string = ''
  38. SET @string2 = ''
  39.  
  40. while @count <= 10
  41. begin
  42.     SET @string = @string + SUBSTRING(@choices,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) AS int)%LEN(@choices)+1,1)
  43.     SET @string2 = @string2 + SUBSTRING(@whatever,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) AS int)%LEN(@whatever)+1,1)
  44.     SET @count = @count + 1
  45. end
  46. print @string
  47. print @string2
  48.  


이올린에 북마크하기(0) 이올린에 추천하기(0)
top


http://www.joon.pe.kr/blog/trackback/308


<< Prev   1   ... 38   39   40   41   42   43   44   45   46   ... 336   Next >>