bitwix

Tangential comments about Software Development

Thursday, October 10, 2013

One-based Indexes are Cra

Yesterday, I imported 88,000 numbers identified with a reference of the format YYYYMMReferenceCurrency e.g. 201308GrossPremiumGBP. But I wanted to process them for a different month, so I went to write a bit of SQL. 

Which of the following is correct:

update table set val = '201309' + substring( val, 6, len(val) - 6 )
update table set val = '201309' + substring( val, 7, len(val) - 7 )

I went for the second one, and got values like 201309GrossPremiumGB. Total fail.

It was a trick question. The answer is of course:

update table set val= '201309' + substring( val, 7, len(val) - 6 )

To replace the first six characters needs a value of 6 and a value of 7. Huh?

I prefer zero-based indexes. I know this is mainly habit, having started in C all those years ago. I like for( i = 0; i < len; i++ ) but it's not much better than for( i = 1; i <= len; i++ ) so that doesn't persuade 1-based people. I wonder whether yesterday's example might change their minds.