SQLite Forum

create unique index and order by not really unique or ordered properly
Login
My testing was in version 3.23...

Here are my tables:

```
CREATE TABLE Answers (AnswerID, Answer);
CREATE UNIQUE INDEX idx_answers on answers (AnswerID);
```

My application `insert or replace` rows in the DB. And then I started to notice some weird behavior only to determine that it was the side effects of the data. Looking at the data:

The `order by` failed here. The `'-'` is here to identify if there are embedded whitespace.

```
sqlite> select '-'||answerid||'-' from answers order by answerid;
-1030-
-1040-
-1060-
-1250-
-1270-
-1350-
-1030-
-1070-
-1120-
```

In the following SQL `distinct` did clean the duplicates but the order failed.

```
sqlite> select distinct '-'||answerid||'-' from answers order by answerid;
-1030-
-1040-
-1060-
-1250-
-1270-
-1350-
-1070-
-1120-
```

I'm drawing a blank on how to fix this or if it's encoding how to identify and repair or prevent.