5

Can I have a SQL query to get the data from columna in tableA whose values don't start with 'f' ?

For example:

select * from  tableA where columnA

where values don't start with letter 'F'.

1
  • What database engine are you using? MSSQL? May 20, 2010 at 4:08

5 Answers 5

9

For a MSSQL Scenario, you should be able to use the "NOT" operator in conjunction with the LIKE operator. So your SQL would look roughly like

select * from tableA where columnA NOT LIKE 'F%'

1
  • 1
    Actually, MSSQL is case INsensitive. You assume he wants [fF], but he might just want 'F', or 'f'. I think you can achieve this for a transaction by changing a setting though -- but it isn't the default. May 20, 2010 at 4:25
5

@Evan: the statement about SQL Server being case insensitive is actually not entirely true. Case sensitivity depends on collation. The server has a collation (chosen on install), a database has a collation (chosen on DB creation) and text columns have a collation (chosen when creating the column). When no collation is specified on DB creation, the server collation will be the default. When no collation specified on column creation it gets the same collation as the DB.

But in most cases, people (luckily) install their server using a case insensitive collation, such as Latin1_General_CI_AS. CI = case insensitive, AS = accent sensitive.

On SQL Server, if I needed to get both the small f and capital F, I would go for:

where columnA NOT LIKE 'F%' and columnA NOT LIKE 'f%'

PS: I'm adding this as "answer" because I don't see any option to comment on an existing answer - I'm still new here... If anyone has an explanation why I don't get this option, don't hesitate to contact me.

Regards, Valentino.

1
  • Thanks, just saw it in the FAQ as well (yeah, rtfm, I know :-)) May 20, 2010 at 6:58
4
SELECT columnA
  FROM tableA
 WHERE SUBSTR(columnA,1,1) <> 'f'

If you need both 'f' and 'F':

SELECT columnA
  FROM tableA
 WHERE SUBSTR(columnA,1,1) NOT IN ('f','F')

Going off of Lerxst's example, some DBMSs will also let you do fun stuff like this:

SELECT columnA
  FROM tableA
 WHERE columnA NOT LIKE ALL ('f%','F%')
2
  • 1
    The NOT IN is categorically a bad idea compaired to NOT LIKE 'F%' May 20, 2010 at 4:24
  • On the last example you use LIKE on a single character. Is that invitational? Is that better than NOT IN, or even NOT LIKE ALL ('f','F')?
    – Kobi
    May 20, 2010 at 4:39
1

I like all of the ideas above, but I usually take a different approach.

SELECT *
FROM tableA
WHERE LEFT(columnA,1) <> 'F'

T-SQL really offers a million ways to skin a cat.

1

Searching for both F and f seems like way too much work

SELECT *
FROM tableA
WHERE upper(substr(columnA,1,1)) != 'F'


Or to quote my friend Ritchie - when searching in sql, trim it and then force it upper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.