SQL Server Performance

Need Help with Like

Discussion in 'SQL Server 2005 General Developer Questions' started by shankbond, Jan 12, 2010.

  1. shankbond New Member

    Hi,
    I have a little problem here, I a having atable with columna VARCHAR(50), This column is having say 400-500 records each record may be starting from alpahabet or numeric or special characters. Now, I don't want any record starting with alphabet, So is there a way by which I can improve the query below?

    SELECT * FROM Table1 WHERE
    columna NOT LIKE 'A%'
    AND columna NOT LIKE 'B%'
    AND columna NOT LIKE 'C%'
    AND columna NOT LIKE 'D%'
    AND columna NOT LIKE 'E%'
    AND columna NOT LIKE 'F%'
    AND columna NOT LIKE 'G%'
    AND columna NOT LIKE 'H%'
    AND columna NOT LIKE 'I%'
    AND columna NOT LIKE 'J%'
    AND columna NOT LIKE 'K%'
    AND columna NOT LIKE 'L%'
    AND columna NOT LIKE 'M%'
    AND columna NOT LIKE 'N%'
    AND columna NOT LIKE 'O%'
    AND columna NOT LIKE 'P%'
    AND columna NOT LIKE 'Q%'
    AND columna NOT LIKE 'R%'
    AND columna NOT LIKE 'S%'
    AND columna NOT LIKE 'T%'
    AND columna NOT LIKE 'U%'
    AND columna NOT LIKE 'V%'
    AND columna NOT LIKE 'W%'
    AND columna NOT LIKE 'X%'
    AND columna NOT LIKE 'Y%'
    AND columna NOT LIKE 'Z%'
  2. FrankKalis Moderator

    WHERE columna LIKE '[0-9]%'
  3. FrankKalis Moderator

    Ooh, since you've mentioned special characters as well, this may be better:
    NOT LIKE '[a-Z]%'
  4. shankbond New Member

    Thanks a lot
  5. venkatesanj@hcl.in New Member

    SELECT * FROM Table1 WHERE
    columna NOT LIKE '[A-Z]%'
    Cheers,
    Venkatesan Prabu .J
  6. Madhivanan Moderator

    [quote user="venkatesanj@hcl.in"]
    SELECT * FROM Table1 WHERE
    columna NOT LIKE '[A-Z]%'
    Cheers,
    Venkatesan Prabu .J
    [/quote]
    Provided the Column has case sensitive collation. Otherwise it would work as Frank specified

Share This Page