Amazon SimpleDb Basics in C# - Zero Padding

If you’re like me and are a developer used to working with relational, typed databases in C#, inevitably one issue will pop up as you write your code. You’ll be sitting there with a bunch of numeric data in your domain (table) and do a comparison or sort based on an integer. And the results will be f***ed up. If you’re a typical developer, you probably didn’t read all of the instructions on SimpleDb and that will be why your code failed. I didn’t either.

You see, attributes (columns) in SimpleDb aren’t typed. And there is no type inference. Everything is a sequence of characters. So ‘2’ will come after ‘10’. Even though, you know, it’s supposed to be before. And thus zero padding has entered your life. Forever. This is why we can’t have nice things.

So here’s what you need to do.

[sourcecode language=“csharp”] selectParameters["Action"] = "Select"; selectParameters["SelectExpression"] = string.Format("SELECT count(*) FROM DomainName where Attribute > ‘{0}’", myDoubleNumber.ToString("D19")); selectParameters["DomainName"] = DomainName; [/sourcecode]

This snippet of code shows how to do a numerical comparison on a double named myDoubleNumber. myDoubleNumber.ToString(“D19”) says that we want to make sure that the number has 19 digits during its conversion to a string. In the case of the single digit ‘2’, 18 zeros will be placed in front of it.

So the number: ‘2’ will be displayed as ‘0000000000000000002’

Got it? If you need more options or help, check out the MSDN Documentation on it.

Also, check out Amazon’s Query 201: Tips and Tricks for Amazon SimpleDB Query for more gotchas on querying SimpleDb data.