Monday, April 20, 2009

[SQL] Counting Rows with the Same Value of a Given Field

Here's a piss-easy script to count the number of rows in a table where a given field has the same value:

SELECT myField, COUNT(*) AS 'inelegantCount'
FROM myTable
GROUP BY myField

It's the classic GROUP BY statement example - which I can't manage to remember, thus the blog post.


Friday, April 10, 2009

Start Using WikiSearch Already!

When I run a search I often have a peek at other people's favourite search results for that given search (from the "see all notes for this WikiSearch" link) in the hope I won't have to spend precious seconds/minutes to find what I am looking for. Unfortunately - even if WikiSearch has been out for a while now - the usual result is the void.

It's well known that any respectable developer spends a fair share of time looking up stuff on Google. Wouldn't it be easier for everyone if we all started using WikiSearch? 

Telling the good stuff from the crap would be much faster.

Friday, April 3, 2009

Definitive Javascript RegEx Validation for Butchers

This comes back to bother me now and then - so I decided to put together a few snippets to use as base in case of client validation with RegExes.

First of all the core snippet, which takes text to validate and a regex and returns a boolean:

function regexMatch(regEx, stringToValidate)
{
var oREGEXP = new RegExp(regEx);
return oREGEXP.test(stringToValidate);
};

Often you will be validating a text field - so here's other two functions using the previous one:

function textFieldVisualRegexValidation(textElement, regEx)
{
var returnValue = false;

if (regexMatch(regEx, textElement.value))
{
textElement.style.backgroundColor = "green";
returnValue = true;
}
else
{
textElement.style.backgroundColor = "red";
}

return returnValue;
};

function textFieldRegexMatch(ctrlName, regEx)
{
var elem = document.getElementById(ctrlName);

return textFieldVisualRegexValidation(elem, regEx);
};

Some event will call textFieldRegexMatch triggering the validation. You can customize textFieldVisualRegexValidation to perform some action in case of validation succeeded or failed (I am setting the input field background to green.red but one could swap images or whatever).

You can hook up the above from (for example) the onBlur event of one of your textBoxes or any input field:

onblur="textFieldRegexMatch('yourInputFieldID', regExPattern)"

You obviously have to declare somewhere your regExPattern:

const regExPattern = "^[A-Z,a-z,0-9]{1,12}$";

I am sure there are better ways of doing the above but this is just meant as a reference to brutally Copy-Paste and tailor to your needs.

Saturday, March 28, 2009

Typing Monkeys and Genetic Algorithms

Recently I spent time playing with generic algorithms. I was looking for something easy enough to implement (read: PLAIN LAZY) and I found the Hello World! (literally) of genetic algorithms.

Gathering the algorithm itself from the C++ example (in the link above), I re-designed and re-coded the whole thing in C#.

TypingMonkey is a very simple application which uses a number (defined by the user) of random strings to evolve the user input text.

From the screen-shot below you can see how it took the algorithm 87 generations to evolve the string ".NET Butchering" from an initial population of 2500 random 15-characters strings:



Why "typing monkey"? Inspired by the Infinite Monkey Theorem, I am pretending our population of random string is generated by a typing monkey:

/// The Typing monkey generates random strings - can't be static 'cause it's a monkey.
/// If you wait long enough it will eventually produce Shakespeare.
class TypingMonkey
{
private const string legalCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.";

static Random random = new Random();

/// The Typing Monkey Generates a random string with the given length.
public string TypeAway(int size)
{
StringBuilder builder = new StringBuilder();
char ch;

for (int i = 0; i < size; i++)
{
ch = legalCharacters[random.Next(0, legalCharacters.Length)];
builder.Append(ch);
}

return builder.ToString();
}
}

You can find the source code here: http://github.com/JohnIdol/typingmonkey/tree/master

Feel free to contribute or despise.

Wednesday, March 4, 2009

How to figure out which SQL2000 edition is installled

I recently had the problem to understand which edition (standard/developer) was installed on an old machine. This sounds like a pretty straightforward thing to do - and it is indeed IF you know you need to run the following query:


SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')


You'd imagine you could figure this out looking at the 'about' information in enterprise manager (as in SQL2005 in SQL Management Studio) but that doesn't seem to be the case. I couldn't find any other solution.
Mysteries of MS faith.