Question: Using the built in REVERSE function on a nvarchar I expect '51.02' to be returned but instead receive 2.

```
DECLARE @HourMins nvarchar = '20.15'

SELECT REVERSE(@HourMins) digs

```

Can anyone tell me why?

[![enter image description here](https://i.stack.imgur.com/sAlpu.png)](https://i.stack.imgur.com/sAlpu.png)

Answer: 
The problem is that you miss combinations such as 2+6+2 (where a lesser number comes after a greater number), because you run (for example) `table[10] += table[10-2]` *before* `table[8] += table[8-6]`, so the former doesn't take into account the result of the latter.

To fix that, change this:

```
    for (i = 2; i <= n; i++)
        table[i] += table[i - 2];
    for (i = 3; i <= n; i++)
        table[i] += table[i - 3];
    for (i = 6; i <= n; i++)
        table[i] += table[i - 6];

```

to this:

```
    for (i = 1; i <= n; i++) {
        if (i >= 2) {
            table[i] += table[i - 2];
        }
        if (i >= 3) {
            table[i] += table[i - 3];
        }
        if (i >= 6) {
            table[i] += table[i - 6];
        }
    }

```

so that you handle e.g. `table[10]` only after you've fully processed e.g. `table[8]`.
Here you go,

**For input 0 or 1, result should be 0, because no way to reach the score using 2, 3 or 6.** 

I consider all the `tiny` numbers `( <= 6 )` first and then find all other numbers with a straight loop (no more if statements).

The returned type is long just to handle large inputs.

```
  if (n == 0 || n == 1)
     return 0L;

  if (n == 2 || n == 3 || n == 4)
     return 1L;

  if (n == 5)
     return 2L;

  if (n == 6)
     return 3L;

  // Otherwise, n>6

  long[] table = new long[n + 1];
  table[0] = 1; // Base case
  table[1] = 0L; //
  table[2] = 1L; // 2
  table[3] = 1L; // 3
  table[4] = 1L; // 2+2
  table[5] = 2L; // 2+3, 3+2
  table[6] = 3L; // 2+2+2, 3+3, 6

  for (int i = 7; i <= n; i++)
     table[i] = table[i - 2] + table[i - 3] + table[i - 6];

  return table[n];

```