Wednesday, April 11, 2012

Pick One Random Number from a Set of Numbers

A while back I posted a means to generate random numbers in SQL. I ran across a situation where I need to pick one number randomly from a set of numbers.

I found a decent solution here, but I didn't want to create a separate view and scalar function for this task.

Instead I create a table variable with a single integer column as the primary key. Next, I insert each of the values of the small set of numbers into the table variable. Finally, I perform a SELECT against the table variable with NEWID() specified in the ORDER BY clause, so that a randomly-chosen member of the set will be returned. 

        DECLARE @Code int

        DECLARE @Codes TABLE
        (
            Code int PRIMARY KEY
        )
       
        INSERT INTO @Codes (Code) VALUES (3)  
        INSERT INTO @Codes (Code) VALUES (6)
        INSERT INTO @Codes (Code) VALUES (9)
        INSERT INTO @Codes (Code) VALUES (12)

        IF @Code IS NULL
            SELECT TOP 1 @Code = Code FROM @Codes ORDER BY NEWID()

  
In my case this is to provide a stored procedure, which may or may not have a value provided for the @Code input parameter, be able to automatically pick a random value in case @Code is NULL. The table variable isn't necessary if the value being picked is among the primary key values of an existing table; a random selection can be obtained simply by doing a SELECT with NEWID().








Thursday, March 22, 2012

Disclaimer

All data and information provided on this site is for informational purposes only. 

DarthContinent.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.

There, now that the legal mumbo jumbo is out of the way, to put it more succinctly, read this at your own risk. I absolutely will not be responsible for any misuse of information contained wherein which happens to be detrimental to the well being of yourself, your Facebook friends, your in-laws, nor anyone else in the whole world, in perpetuity. 

Even the Jedi have no sway here! 

They, too, are bound by galactic law, despite their deep desire to eradicate the Sith. Indeed, were the Jedi to attempt to take this site down by Force, figuratively or literally, I would use the Sith powers at my disposal to squish their testicles or ovaries into raisin-like remnants of their former glory! Then would they know the TRUE power of the Force and the Dark side!

Well. Enough of that, let's party!






Friday, February 10, 2012

Freaky Friday, Reddit Style

Not like this, Reddit. Not like this. Once again, the Reddit-headed step child appears to be looming as inexplicably comments have ceased to be visible as of around 10:30 am EST.


Have the admins tapped a keg early? Has a lowly intern been given the keys to admin access enabling them to wreak havoc upon the database?? Who knows. Perhaps Reddit needs a makeover.


 

Thursday, February 2, 2012

Cents and Sensibility

I recently received a check in the mail for the eBay vs Yingling class-action lawsuit.















This makes no cents whatsoever.