Amass

How to Left-Pad a field in MSSQL

May 28th, 2008 Luke

I recently encountered a situation where I needed to left-pad a string to a certain number of characters with 0’s. This is the SQL to do it:

RIGHT('000' + rtrim(column_name),3)

The ‘000′ is your padding characters, column_name is what you’re padding, and the 3 is how many characters you want back.

Creating arrays of strings on-the-fly

May 23rd, 2008 Luke

Sometimes you’ll encounter a situation where you need an array of strings, just so that you can pass it into a function. Doing it in two lines is ugly if it’s just a temporary variable - and it tends to pollute your Intellisense. Here’s how to do it in one:

new string[3]{"foo","bar","baz"};

Let’s say you were calling a function that took in an array of strings. You could use it like this:

myFunction(new string[3]{"foo","bar","baz"});

And you’d be good to go!