1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

query containing entries that begin with same 3 letters as at least one other entry?

Discussion in 'SQL - Questions and Answers' started by Michael, Sep 7, 2006.

  1. Michael Guest

    Dialect: MS SQL
    How do i create a table from another table containing only those beginning with the same three letters as another entry in the table
    so...
    abc hello
    abc goodbye
    ghj must
    ghjsomthing

    would all be in the new table
  2. Dimitar Guest

    Dialect: MS SQL
    SQL:

    insert into dups select * from scores where substring(player from 1 for 3) in (select substring(player from 1 for 3) from scores group by substring(player from 1 for 3) having count(*) > 1);

    Please, do not hesitate to ask for clarifications if so is needed.
    I would be glad to hear back from you whether the above worked or not.

    Best Regards,

    Dimitar
  3. Dimitar Guest

    Dialect: MS SQL
    Not really an answer, but perhaps a hint: check the manual for whether the is supported by MS SQL. If so it would replace the rightmost select with the group by and having clause, like this
    SQL:

    ... unique (select substring(player from 1 for 3) from scores)
    .
    Hopefully it will be faster.

Share This Page