Damian Maclennan Blog

Damian's Datalog

Fun with the SPACE function

I'm doing a little bit of ASP maintainance work for the company I'm contracting with at the moment. I had a small task to indent items in a drop down select list in line with their level in a hierarchy tree. Nothing difficult there except that the page used a generic function to draw select lists. The last thing I want to do is reimplent it for this one off case so I decided to do it in SQL. T-SQL Has a function "SPACE(i)" which returns a number of whitespace characters. Once you have that you can replace it with whatever you like. Here is what I added to the stored procedure :

Replace(SPACE ( (level - 1) * 2 ), ' ', ' ') + label as indentLabel

So I could pass the "indentLabel" column off to the function instead of "label" and all is good, I have a nicely indented select box.

It's pretty simple, but I told Rob about it and got the reaction "Never would've thought of that". So there you go.