Friday, June 15, 2007

concatenating strings and nulls in SQL

let's start with a table from scratch

create table test(
firstname varchar(10) null,
lastname varchar(10) null

we then insert some data (sorry about people with these names)

insert into test values ('john', 'smith')
insert into test values ('john', 'doe')
insert into test values (null, 'perez')

we're ready to concatenate some strings:

select lastname + ' ' + firstname
from test

but we get:

1 smith john
2 doe john

what happened?

in SQL, when you concatenate strings, if one of the strings is null, the result of the concatenation is null.

At first you might think this is bad, but it can actually help you more than it hurts, you just have to know the trick, so what do we do to fix the query?

select isnull(lastname, '') + ' ' + isnull(firstname, '')
from test

now we get:

1 smith john
2 doe john
3 perez

ok, but how is that helpful?

well, let's suppose you want this format:

lastname, first name

but only if there is a first name, if the first name is null you just want the last name with no comma

let's say there is a rule on the database that the last name cannot be null, so we can write the query as:

select lastname + isnull(', '+firstname, '')
from test

and we get:

1 smith, john
2 doe, john
3 perez

see, I concatenated the comma with the first name, and if the first name is null, the the comma gets eliminated as well

Just one more trick for your bag of tricks

Tuesday, June 12, 2007

C# quiz #2: objects, strings, null

this is a continuation from the previous quiz; given this class definition:

public class Foo {
    public void Bar(object x) {
        Console.WriteLine("object x was called");
    public void Bar(string x) {
        Console.WriteLine("string x was called");

what will be output to the console with the following code:

Foo f = new Foo();
try {
catch {
    Console.WriteLine("no method was called");

Extra points for explaining why

Monday, June 11, 2007

how to tell google to NOT load the country specific page

Scoble is having this issue right now, I've had it in the past and forgotten to post this here, google tries to play smart and load the country specific page, so if you are in Mexico and you go to, you'll get instead, to fix it just go to this url

Sunday, June 10, 2007

Cheap tricks to get a more popular blog

There are a few tricks that I have seen used more and more lately to try to get more attention to blogs

1) use adult content related words to refer to some technical topic (but not necessarily), some recent examples of such use recently:
  1. Computer Hardware Pornography
  2. ...What you're not getting about Ruby and why it's the tits
  3. "As a gay man and a gay journalist"
  4. "..Cierra la puta boca, gilipollas, Siesta, Fiesta, Cerveza, Real Madrid, Raúl, Hijo de Puta y Mierda."
you are likely to get some "google love" for sure with those keywords

2) get in a debate with some "big guy", this technique is getting used more and more lately, we have
Jeff Atwood vs Petzold's
Ayende vs Ted Neward's
Ayende vs Sam
Rob Conery vs Ayende
Nick vs Sam
heck, Ayende vs the rest of the world, J

etc, all you have to do is talk trash about something a guy posted on his blog, or about some of the work (usually open source) the guy works on, if your blog is not very popular, make sure to leave a comment on the other blog so the guy notices you

3) ask for link love, this specific case actually has a merit, this should be listed under "how to get a popular blog in 10 days", but the idea is the same

sigh... boring stuff, but I guess it works if all you want is to get more people to your blog (inflate your numbers), even if what they find is not exactly what they were looking for (first case)

Saturday, June 09, 2007

programming practices: never say never

Jan Bannister wrote this post: bool considered harmful, and writes:
Never Use Bool, or more specifically never user bool as a parameter. It is the most foolish of datatypes and conveys the smallest amount of information possible.
I can disagree with that in a number of ways, but the culprit was the "never use bool as a parameter", the biggest problems I see
- you would end up with hundreds of enums to replace your bool parameters, and then
- where do you put all those enums?
- at some point you would probably duplicate some of the required functionality

so, what do we do, bool is still "harmful", but not always, that's the key, I can think of at least 2 situations where you don't necessarily need to replace your bools with enums
- private methods: they are (hopefully) only used in that class... and the method is not that big, and the class is not that big, and you added comments for the method/parameters... sigh...
- functions with a single parameter that is just an on/off thing, e.g. SetVisible(true)

there, now it doesn't sound that bad, bottom line, never say never, don't abuse any technique

C# Quiz #1: overloads, strings, nullable types

*This is also a test of the latest Windows Live Writer

given this class definition:

public class Foo {
public void Bar(string x) {
Console.WriteLine("string x was called");
public void Bar(int? x) {
Console.WriteLine("int? x was called");

what will be output to the console with the following code:

Foo f = new Foo();
try {
catch {
Console.WriteLine("no method was called");

*update: I messed up the code while trying to upload another post, I recovered the original post from my RSS feed

Wednesday, June 06, 2007

Filemon saves the day once again

I'm back, I had a head injury during a soccer game, I was very dizzy for over a week, but it's all good now

This time it was an ActiveX control (we're still fighting with ActiveX controls), I thought I had done everything I needed to do to get it up and working, but it would simply not load, so I loaded Filemon to see if it was even referencing the dll file, and it found two references, one to the GAC folder, and one to the program files\Internet Explorer, but no reference to where we actually copied the dll.

So I decided to copy the dll to where IE was looking for it, I copied the file to
c:\program files\Internet Explorer\
and everything started working wonderfully

I guess I could've added it to the GAC too, but I was very frustrated at that point since I thought I had tried that before and still wasn't working.

anyway... I guess that's another trick you can try when you have tried everything else deploying your ActiveX control, use filemon to see where IE is actually trying to load your dll from, and copy it there

and if you still don't know what filemon is, you owe it to your self, it's an amazing tool, it will save you many hours of headache (just not the ones caused by injuries during a soccer game J)