Assuming that you do not wish to modify the content of the table (and hence get slightly less efficient queries), the following should do the trick.
(If you do have the leisure of modifying the table, see the suggestions at the end of this answer)
SELECT Title
FROM myTable
WHERE (Title LIKE 'x%' OR Title LIKE 'THE x%')
-- AND Title NOT LIKE 'THE [^T]%'
ORDER BY Title
Notes:
- x designate the desired letter (example: LIKE 'A%' etc.)
- The "AND TITLE NOT LIKE" extra condition is only needed when "X" is the letter "T" (it is otherwise functionally redundant, but doesn't change the result)
- I'm unsure of the support of the the [^xyz]
(i.e. NOT characters x, y or z), so the [^T]
could be replaced by its positive equivalent say [A-RS-Z0-9]
.
There are a few other stop words to consider ("A", "AN", "OF"...) but for book or film titles, it is a common practice to only consider "THE". If you must deal with the other articles, the logic can be extended as in:
SELECT Title
FROM myTable
WHERE (Title LIKE 'x%'
OR Title LIKE 'THE x%'
OR Title LIKE 'A x%'
OR Title LIKE 'AN x%')
-- the following is only needed when "x" is either the letter T or A.
-- AND (Title NOT LIKE 'THE [^T]%'
-- AND Title NOT LIKE 'A [^A]%'
-- AND Title NOT LIKE 'AN [^A]%'
-- )
ORDER BY Title
There are better solutions, if you can modify the table's contents. Some of these imply pre-computing one or several extra columns (and maintaining it/these, when new records are added etc.).
- See for example Cletus' answer in this post for the "sort_column" approach, where the extra column contains the title stripped of any undesirable leading noise-word. In addition to its purpose as the filtering field in the initial letter search problem of the OP, this column can also be used to sort, in a more friendly/sensible way, lists of titles which were produced by a filter unrelated to the initial letter and/or the begining of the title (say a search by year).
- A variation on the above is to only store the "effective" initial letter (the one past the undesired noise), making for a smaller column, but a less versatile one too.
- The title column itself can be updated, storing a modified form of the title, whereby the extraneous leading noise-word(s) is moved to the end of the string, between parenthesis. This practice is quite common with bibliographic-type catalogs.