<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Random Number Generator Script &#8211; SQL Query</title>
	<atom:link href="http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:54:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Dennis A</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-57363</link>
		<dc:creator>Dennis A</dc:creator>
		<pubDate>Thu, 05 Nov 2009 17:35:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-57363</guid>
		<description>Additionally you can replace the use of the Char() function with a SubString.

Example
SubString(&#039;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&#039;, dbo.Rnd(1,36), 1)

For a random digit or letter and that list could obviously be extended to include any characters you wish.</description>
		<content:encoded><![CDATA[<p>Additionally you can replace the use of the Char() function with a SubString.</p>
<p>Example<br />
SubString(&#8216;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&#8217;, dbo.Rnd(1,36), 1)</p>
<p>For a random digit or letter and that list could obviously be extended to include any characters you wish.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dennis A</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-57362</link>
		<dc:creator>Dennis A</dc:creator>
		<pubDate>Thu, 05 Nov 2009 17:24:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-57362</guid>
		<description>You can solve random string generation using a mask and substitution.  There are many ways to solve this issue, but if we break them down into 3 parts, it will make our solution more flexible.

Part 1 - The randomizer view, this is nothing new, just generating a random float in a view so it can be used in a function.
CREATE VIEW [dbo].[Randomizer] AS
SELECT abs((convert(bigint,convert(binary(8),newid()))*0.000000000000001)%1.0) AS n

Part 2 - A Random function that takes min and max parameters so that we can generate a random value within desired bounds.

CREATE FUNCTION dbo.Rnd( @min bigint, @max bigint ) RETURNS bigint AS
BEGIN

	RETURN convert(bigint, 
			(@max - @min + 1)
			*
			(SELECT TOP 1 n FROM dbo.Randomizer)
			+ 
			@min
			);	
END

Part 3 - A generic mask substitution function so that we can ask specify any mask we want.  I will only implement the very most basic set of substitutions.  You can extend these in simple ways. 
CREATE FUNCTION dbo.RandomText( @mask varchar(64) ) RETURNS varchar(64) AS
BEGIN
-- Goal: Substatute mask tokens with random values
-- Inputs: @mask - a string with subtatution tokens and literals
-- Tokens: # - will be replaced with a random digit [0-9]
--         A - will be replaced with a random upper case english letter [A-Z]

SELECT
	@mask = Stuff( @mask, start, 1, replacement )
FROM (
	SELECT 
		n AS start,
		CASE SubString( @mask, n, 1 )
			WHEN &#039;#&#039; THEN char( dbo.Rnd(48,57) ) -- [0-9]
			WHEN &#039;A&#039; THEN char( dbo.Rnd(65,90) ) -- [A-Z]
		END AS replacement
	FROM (
		SELECT (1+n1.n+n10.n) AS n
		FROM 
			(SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) n1
			CROSS JOIN (SELECT 0 AS n UNION SELECT 10 UNION SELECT 20 UNION SELECT 30 UNION SELECT 40 UNION SELECT 50 UNION SELECT 60) AS n10
		WHERE 
			(1+n1.n+n10.n) BETWEEN 1 AND Len(@mask)
		) sequence
	WHERE
		SubString( @mask, n, 1 ) IN (&#039;#&#039;,&#039;A&#039;)
	) substatutions

RETURN @mask

END</description>
		<content:encoded><![CDATA[<p>You can solve random string generation using a mask and substitution.  There are many ways to solve this issue, but if we break them down into 3 parts, it will make our solution more flexible.</p>
<p>Part 1 &#8211; The randomizer view, this is nothing new, just generating a random float in a view so it can be used in a function.<br />
CREATE VIEW [dbo].[Randomizer] AS<br />
SELECT abs((convert(bigint,convert(binary(8),newid()))*0.000000000000001)%1.0) AS n</p>
<p>Part 2 &#8211; A Random function that takes min and max parameters so that we can generate a random value within desired bounds.</p>
<p>CREATE FUNCTION dbo.Rnd( @min bigint, @max bigint ) RETURNS bigint AS<br />
BEGIN</p>
<p>	RETURN convert(bigint,<br />
			(@max &#8211; @min + 1)<br />
			*<br />
			(SELECT TOP 1 n FROM dbo.Randomizer)<br />
			+<br />
			@min<br />
			);<br />
END</p>
<p>Part 3 &#8211; A generic mask substitution function so that we can ask specify any mask we want.  I will only implement the very most basic set of substitutions.  You can extend these in simple ways.<br />
CREATE FUNCTION dbo.RandomText( @mask varchar(64) ) RETURNS varchar(64) AS<br />
BEGIN<br />
&#8211; Goal: Substatute mask tokens with random values<br />
&#8211; Inputs: @mask &#8211; a string with subtatution tokens and literals<br />
&#8211; Tokens: # &#8211; will be replaced with a random digit [0-9]<br />
&#8211;         A &#8211; will be replaced with a random upper case english letter [A-Z]</p>
<p>SELECT<br />
	@mask = Stuff( @mask, start, 1, replacement )<br />
FROM (<br />
	SELECT<br />
		n AS start,<br />
		CASE SubString( @mask, n, 1 )<br />
			WHEN &#8216;#&#8217; THEN char( dbo.Rnd(48,57) ) &#8212; [0-9]<br />
			WHEN &#8216;A&#8217; THEN char( dbo.Rnd(65,90) ) &#8212; [A-Z]<br />
		END AS replacement<br />
	FROM (<br />
		SELECT (1+n1.n+n10.n) AS n<br />
		FROM<br />
			(SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) n1<br />
			CROSS JOIN (SELECT 0 AS n UNION SELECT 10 UNION SELECT 20 UNION SELECT 30 UNION SELECT 40 UNION SELECT 50 UNION SELECT 60) AS n10<br />
		WHERE<br />
			(1+n1.n+n10.n) BETWEEN 1 AND Len(@mask)<br />
		) sequence<br />
	WHERE<br />
		SubString( @mask, n, 1 ) IN (&#8216;#&#8217;,'A&#8217;)<br />
	) substatutions</p>
<p>RETURN @mask</p>
<p>END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-57035</link>
		<dc:creator>John</dc:creator>
		<pubDate>Mon, 26 Oct 2009 13:32:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-57035</guid>
		<description>Great article.  What I need is a way to product int char char int. Example: 0AA0 - Position 1 - a number between 1-9, Position 2 - a letter between A-Z, Position 3 - a letter between A-Z, Position 4 - a number between 1-9.

Very new to SQL, T-SQL - so be kind.  :-)</description>
		<content:encoded><![CDATA[<p>Great article.  What I need is a way to product int char char int. Example: 0AA0 &#8211; Position 1 &#8211; a number between 1-9, Position 2 &#8211; a letter between A-Z, Position 3 &#8211; a letter between A-Z, Position 4 &#8211; a number between 1-9.</p>
<p>Very new to SQL, T-SQL &#8211; so be kind.  :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-56791</link>
		<dc:creator>Brian Tkatch</dc:creator>
		<pubDate>Mon, 19 Oct 2009 14:34:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-56791</guid>
		<description>@sharry

Use RAND() to get a number between 1 and 9, and use the number as an index into your number string.

If the number is given as a long string with varying lengths, you may need to expand the list first. There is a good article on how to do that here: http://bradsruminations.blogspot.com/2009/10/un-making-list-or-shredding-of-evidence.html</description>
		<content:encoded><![CDATA[<p>@sharry</p>
<p>Use RAND() to get a number between 1 and 9, and use the number as an index into your number string.</p>
<p>If the number is given as a long string with varying lengths, you may need to expand the list first. There is a good article on how to do that here: <a href="http://bradsruminations.blogspot.com/2009/10/un-making-list-or-shredding-of-evidence.html" rel="nofollow">http://bradsruminations.blogspot.com/2009/10/un-making-list-or-shredding-of-evidence.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sharry</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-56769</link>
		<dc:creator>sharry</dc:creator>
		<pubDate>Sat, 17 Oct 2009 08:48:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-56769</guid>
		<description>suppose i wan a random number between 1,2,3,4,5,12,13,34,23
dis set of numbers den how do i do dat.????
Pls help</description>
		<content:encoded><![CDATA[<p>suppose i wan a random number between 1,2,3,4,5,12,13,34,23<br />
dis set of numbers den how do i do dat.????<br />
Pls help</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Luca</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-56520</link>
		<dc:creator>Alex Luca</dc:creator>
		<pubDate>Thu, 08 Oct 2009 07:07:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-56520</guid>
		<description>Hello.

I think the first script, is flawed.
This one:
---- Create the variables for the random number generation
DECLARE @Random INT;
DECLARE @Upper INT;
DECLARE @Lower INT

---- This will create a random number between 1 and 999
SET @Lower = 1 ---- The lowest random number
SET @Upper = 999 ---- The highest random number
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random

If you set a lower limit of 1, and upper limit 2, it will always return one.

If you remove the &#039;-1&#039; from here:  ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0) it returns 1 and 2.

Maybe I am mistaken and this is a particular case.

Thanks for the great site, I found many useful scripts here. :)

Regards,
Alex Luca</description>
		<content:encoded><![CDATA[<p>Hello.</p>
<p>I think the first script, is flawed.<br />
This one:<br />
&#8212;- Create the variables for the random number generation<br />
DECLARE @Random INT;<br />
DECLARE @Upper INT;<br />
DECLARE @Lower INT</p>
<p>&#8212;- This will create a random number between 1 and 999<br />
SET @Lower = 1 &#8212;- The lowest random number<br />
SET @Upper = 999 &#8212;- The highest random number<br />
SELECT @Random = ROUND(((@Upper &#8211; @Lower -1) * RAND() + @Lower), 0)<br />
SELECT @Random</p>
<p>If you set a lower limit of 1, and upper limit 2, it will always return one.</p>
<p>If you remove the &#8216;-1&#8242; from here:  ROUND(((@Upper &#8211; @Lower -1) * RAND() + @Lower), 0) it returns 1 and 2.</p>
<p>Maybe I am mistaken and this is a particular case.</p>
<p>Thanks for the great site, I found many useful scripts here. :)</p>
<p>Regards,<br />
Alex Luca</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tejpal</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-55444</link>
		<dc:creator>Tejpal</dc:creator>
		<pubDate>Tue, 01 Sep 2009 02:14:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-55444</guid>
		<description>Hi 

I have table with 100 records, how can I update a  field on this with random numbers from 1 thru 100, when I do this using RANd() I do get duplicate random numers in the file which does not  help me. Any help?</description>
		<content:encoded><![CDATA[<p>Hi </p>
<p>I have table with 100 records, how can I update a  field on this with random numbers from 1 thru 100, when I do this using RANd() I do get duplicate random numers in the file which does not  help me. Any help?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: andy</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-54882</link>
		<dc:creator>andy</dc:creator>
		<pubDate>Sun, 16 Aug 2009 00:35:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-54882</guid>
		<description>Great help this will come in really handy for my latest application 

Love the blog.</description>
		<content:encoded><![CDATA[<p>Great help this will come in really handy for my latest application </p>
<p>Love the blog.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phoeneous</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-53974</link>
		<dc:creator>Phoeneous</dc:creator>
		<pubDate>Wed, 22 Jul 2009 23:58:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-53974</guid>
		<description>Great lesson but how can I display all of these in one record?

[code]
DECLARE @Random_Sales INT;
DECLARE @Upper_Sales INT;
DECLARE @Lower_Sales INT;
DECLARE @Random_Profit INT;
DECLARE @Upper_Profit INT;
DECLARE @Lower_Profit INT;
DECLARE @Random_Percent INT;
DECLARE @Upper_Percent INT;
DECLARE @Lower_Percent INT

SET @Lower_Sales = 500000
SET @Upper_Sales = 599999
SET @Lower_Profit = 60000
SET @Upper_Profit = 99999
SET @Lower_Percent = 15
SET @Upper_Percent = 20

SELECT @Random_Sales = ROUND(((@Upper_Sales - @Lower_Sales -1) * RAND() + @Lower_Sales), 0)
SELECT @Random_Sales
SELECT @Random_Sales = ROUND(((@Upper_Sales - @Lower_Sales -1) * RAND() + @Lower_Sales), 0)
SELECT @Random_Sales
SELECT @Random_Profit = ROUND(((@Upper_Profit - @Lower_Profit -1) * RAND() + @Lower_Profit), 0)
SELECT @Random_Profit
SELECT @Random_Percent = ROUND(((@Upper_Percent - @Lower_Percent -1) * RAND() + @Lower_Percent), 0)
SELECT @Random_Percent
[/code]</description>
		<content:encoded><![CDATA[<p>Great lesson but how can I display all of these in one record?</p>
<pre class="brush: plain;">
DECLARE @Random_Sales INT;
DECLARE @Upper_Sales INT;
DECLARE @Lower_Sales INT;
DECLARE @Random_Profit INT;
DECLARE @Upper_Profit INT;
DECLARE @Lower_Profit INT;
DECLARE @Random_Percent INT;
DECLARE @Upper_Percent INT;
DECLARE @Lower_Percent INT

SET @Lower_Sales = 500000
SET @Upper_Sales = 599999
SET @Lower_Profit = 60000
SET @Upper_Profit = 99999
SET @Lower_Percent = 15
SET @Upper_Percent = 20

SELECT @Random_Sales = ROUND(((@Upper_Sales - @Lower_Sales -1) * RAND() + @Lower_Sales), 0)
SELECT @Random_Sales
SELECT @Random_Sales = ROUND(((@Upper_Sales - @Lower_Sales -1) * RAND() + @Lower_Sales), 0)
SELECT @Random_Sales
SELECT @Random_Profit = ROUND(((@Upper_Profit - @Lower_Profit -1) * RAND() + @Lower_Profit), 0)
SELECT @Random_Profit
SELECT @Random_Percent = ROUND(((@Upper_Percent - @Lower_Percent -1) * RAND() + @Lower_Percent), 0)
SELECT @Random_Percent
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: New techie Praveen</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-53904</link>
		<dc:creator>New techie Praveen</dc:creator>
		<pubDate>Tue, 21 Jul 2009 09:20:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-53904</guid>
		<description>Thanks pinal,

You are the best.</description>
		<content:encoded><![CDATA[<p>Thanks pinal,</p>
<p>You are the best.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CFLisa</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-53259</link>
		<dc:creator>CFLisa</dc:creator>
		<pubDate>Thu, 25 Jun 2009 18:17:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-53259</guid>
		<description>Hi - loved this article.  Got an interesting twist, though.  Regarding &#039;Method 5 : Random number on a per row basis&#039; - if I do this:

SELECT ABS(CAST(NEWID() AS binary(6)) %3)

I will always get either a 0, 1, or 2 - perfect!

However, what I am ultimately trying to do is to assign a mapped value to a field depending on the value of the random number generated above, like this:

UPDATE MyTable
SET MyField = 
    CASE ABS(CAST(NEWID() AS binary(6)) % 3)
        WHEN 0 THEN &#039;red&#039;
        WHEN 1 THEN &#039;blue&#039;
        WHEN 2 THEN &#039;green&#039;
    END
WHERE KeyField = &#039;somevalue&#039;

Apparently the CASE statement is getting confused at times, and although in most cases, the field will be be updated with &#039;red,&#039; &#039;blue,&#039; or &#039;green,&#039; at least 30% of the time, the CASE statement returns NULL!  I don&#039;t know why this would possibly be happening, since using ABS(CAST(NEWID() AS binary(6)) % 3) without a CASE statement NEVER returns NULL!

To test this yourself, first trying executing this several times:

SELECT ABS(CAST(NEWID() AS binary(6)) % 3)

You&#039;ll see that you never get a NULL returned.

Next, try executing this several times:

SELECT CASE ABS(CAST(NEWID() AS binary(6)) % 3)
    WHEN 0 THEN &#039;zero&#039;
    WHEN 1 THEN &#039;one&#039;
    WHEN 2 THEN &#039;two&#039;
END

You&#039;ll many times that you get a NULL returned.

HELP!</description>
		<content:encoded><![CDATA[<p>Hi &#8211; loved this article.  Got an interesting twist, though.  Regarding &#8216;Method 5 : Random number on a per row basis&#8217; &#8211; if I do this:</p>
<p>SELECT ABS(CAST(NEWID() AS binary(6)) %3)</p>
<p>I will always get either a 0, 1, or 2 &#8211; perfect!</p>
<p>However, what I am ultimately trying to do is to assign a mapped value to a field depending on the value of the random number generated above, like this:</p>
<p>UPDATE MyTable<br />
SET MyField =<br />
    CASE ABS(CAST(NEWID() AS binary(6)) % 3)<br />
        WHEN 0 THEN &#8216;red&#8217;<br />
        WHEN 1 THEN &#8216;blue&#8217;<br />
        WHEN 2 THEN &#8216;green&#8217;<br />
    END<br />
WHERE KeyField = &#8217;somevalue&#8217;</p>
<p>Apparently the CASE statement is getting confused at times, and although in most cases, the field will be be updated with &#8216;red,&#8217; &#8216;blue,&#8217; or &#8216;green,&#8217; at least 30% of the time, the CASE statement returns NULL!  I don&#8217;t know why this would possibly be happening, since using ABS(CAST(NEWID() AS binary(6)) % 3) without a CASE statement NEVER returns NULL!</p>
<p>To test this yourself, first trying executing this several times:</p>
<p>SELECT ABS(CAST(NEWID() AS binary(6)) % 3)</p>
<p>You&#8217;ll see that you never get a NULL returned.</p>
<p>Next, try executing this several times:</p>
<p>SELECT CASE ABS(CAST(NEWID() AS binary(6)) % 3)<br />
    WHEN 0 THEN &#8216;zero&#8217;<br />
    WHEN 1 THEN &#8216;one&#8217;<br />
    WHEN 2 THEN &#8216;two&#8217;<br />
END</p>
<p>You&#8217;ll many times that you get a NULL returned.</p>
<p>HELP!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Goadorrocurce</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-52129</link>
		<dc:creator>Goadorrocurce</dc:creator>
		<pubDate>Wed, 20 May 2009 20:07:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-52129</guid>
		<description>Neat web site - Will definitely visit again!</description>
		<content:encoded><![CDATA[<p>Neat web site &#8211; Will definitely visit again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-52040</link>
		<dc:creator>Imran Mohammed</dc:creator>
		<pubDate>Tue, 19 May 2009 03:54:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-52040</guid>
		<description>@DM 

LIMIT is not ANSI SQL. Not Especially T-SQL . LIMIT Key word is only in MYSQL.

~ IM.</description>
		<content:encoded><![CDATA[<p>@DM </p>
<p>LIMIT is not ANSI SQL. Not Especially T-SQL . LIMIT Key word is only in MYSQL.</p>
<p>~ IM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DM</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-52014</link>
		<dc:creator>DM</dc:creator>
		<pubDate>Mon, 18 May 2009 18:38:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-52014</guid>
		<description>I have 400K records on my table. Now I wanted you pull out random records that falls every 5th records from top to bottom. How can I do that sql?

I tried using select stmt (below) but I had message error on LIMIT function.

Select stmt:
select * from table_name order by RAND() limit 5

error msg:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near &#039;limit&#039;.</description>
		<content:encoded><![CDATA[<p>I have 400K records on my table. Now I wanted you pull out random records that falls every 5th records from top to bottom. How can I do that sql?</p>
<p>I tried using select stmt (below) but I had message error on LIMIT function.</p>
<p>Select stmt:<br />
select * from table_name order by RAND() limit 5</p>
<p>error msg:<br />
Msg 102, Level 15, State 1, Line 1<br />
Incorrect syntax near &#8216;limit&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-49559</link>
		<dc:creator>Simon</dc:creator>
		<pubDate>Fri, 20 Mar 2009 13:47:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-49559</guid>
		<description>The crucial trick is to add 0.5 (!)
In this way we get no bias against returning the upper and the lower limits

CREATE PROCEDURE GetRandomInteger
(
	@Min	int,
	@Max	int,
	@rv		int OUTPUT
)
AS
BEGIN
	DECLARE @Rand float

	SELECT @Rand = ((@Max - @Min + 1) * RAND(CONVERT(int, CONVERT(varbinary, NEWID())))) + .5 + (@Min - 1)

	SELECT @rv = ROUND(@Rand, 0)

END</description>
		<content:encoded><![CDATA[<p>The crucial trick is to add 0.5 (!)<br />
In this way we get no bias against returning the upper and the lower limits</p>
<p>CREATE PROCEDURE GetRandomInteger<br />
(<br />
	@Min	int,<br />
	@Max	int,<br />
	@rv		int OUTPUT<br />
)<br />
AS<br />
BEGIN<br />
	DECLARE @Rand float</p>
<p>	SELECT @Rand = ((@Max &#8211; @Min + 1) * RAND(CONVERT(int, CONVERT(varbinary, NEWID())))) + .5 + (@Min &#8211; 1)</p>
<p>	SELECT @rv = ROUND(@Rand, 0)</p>
<p>END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-47204</link>
		<dc:creator>SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave</dc:creator>
		<pubDate>Tue, 24 Feb 2009 12:08:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-47204</guid>
		<description>[...] SQL SERVER - Random Number Generator Script - SQL Query [...]</description>
		<content:encoded><![CDATA[<p>[...] SQL SERVER &#8211; Random Number Generator Script &#8211; SQL Query [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pranay</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-46222</link>
		<dc:creator>pranay</dc:creator>
		<pubDate>Mon, 02 Feb 2009 05:52:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-46222</guid>
		<description>hi
I want to select a four different random number from the four different column  of the table and put in in the another column of the table. can any one help me to solve this.
bye</description>
		<content:encoded><![CDATA[<p>hi<br />
I want to select a four different random number from the four different column  of the table and put in in the another column of the table. can any one help me to solve this.<br />
bye</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vaishali R</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-45549</link>
		<dc:creator>Vaishali R</dc:creator>
		<pubDate>Mon, 12 Jan 2009 11:32:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-45549</guid>
		<description>Thank you  ... saved my time</description>
		<content:encoded><![CDATA[<p>Thank you  &#8230; saved my time</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-45303</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Fri, 02 Jan 2009 20:45:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-45303</guid>
		<description>I have a stored procedure that selects a number of rows dependant on various input values - 

for example, say the query selects 12 different rows, how can I then rendomly select one of the twelve and drop the other 11? 

thankyou

Matt</description>
		<content:encoded><![CDATA[<p>I have a stored procedure that selects a number of rows dependant on various input values &#8211; </p>
<p>for example, say the query selects 12 different rows, how can I then rendomly select one of the twelve and drop the other 11? </p>
<p>thankyou</p>
<p>Matt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nagarajan</title>
		<link>http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/#comment-44046</link>
		<dc:creator>Nagarajan</dc:creator>
		<pubDate>Tue, 04 Nov 2008 09:42:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/29/random-number-generator-script-sql-query/#comment-44046</guid>
		<description>Hi,  I have a data table , in which i want only selected (4) records when i press next button  the next set of selected (4) records should be displayed  like paging option in grid view .. is there any query to perform this operation.. so that  loading the entire table in to grid can be avoided...  please give your comments... thanks in advance</description>
		<content:encoded><![CDATA[<p>Hi,  I have a data table , in which i want only selected (4) records when i press next button  the next set of selected (4) records should be displayed  like paging option in grid view .. is there any query to perform this operation.. so that  loading the entire table in to grid can be avoided&#8230;  please give your comments&#8230; thanks in advance</p>
]]></content:encoded>
	</item>
</channel>
</rss>
