Question: Background:

* I have a short list of strings.
* The number of strings is not always the same, but are nearly always of the order of a “handful”
* In our database will store these strings in a 2nd normalised table
* These strings are **never** changed once they are written to the database.

We wish to be able to match on these strings quickly in a query without the performance hit of doing lots of joins.

So I am thinking of storing a hash code of all these strings in the main table and including it in our index, so the joins are only processed by the database when the hash code matches.

So how do I get a good hashcode? I could:

* Xor the hash codes of all the string together
* Xor with multiply the result after each string (say by 31)
* Cat all the string together then get the hashcode
* Some other way

So what do people think?

---

In the end I just concatenate the strings and compute the hashcode for the concatenation, as it is simple and worked well enough.

*(If you care we are using .NET and SqlServer)*

---

**Bug!, Bug!**

[Quoting from Guidelines and rules for GetHashCode](http://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/) by Eric Lippert 

> 
> The documentation for
>  System.String.GetHashCode notes
>  specifically that two identical
>  strings can have different hash codes
>  in different versions of the CLR, and
>  in fact they do. Don't store string
>  hashes in databases and expect them to
>  be the same forever, because they
>  won't be.
> 
> 
> 

So String.GetHashcode() should not be used for this.

Answer: 
Another way that pops in my head, chain xors with rotated hashes based on index:

```
int shift = 0;
int result = 1;
for(String s : strings)
{
    result ^= (s.hashCode() << shift) | (s.hashCode() >> (32-shift)) & (1 << shift - 1);
    shift = (shift+1)%32;
}

```

edit: reading the explanation given in effective java, I think geoff's code would be much more efficient.
A SQL-based solution could be based on the checksum and checksum\_agg functions. If I'm following it right, you have something like:

```
MyTable
  MyTableId
  HashCode

MyChildTable
  MyTableId  (foreign key into MyTable)
  String

```

with the various strings for a given item (MyTableId) stored in MyChildTable. To calculate and store a checksum reflecting these (never-to-be-changed) strings, something like this should work:

```
UPDATE MyTable
 set HashCode = checksum_agg(checksum(string))
 from MyTable mt
  inner join MyChildTable ct
   on ct.MyTableId = mt.MyTableId
 where mt.MyTableId = @OnlyForThisOne

```

I believe this is order-independant, so strings "The quick brown" would produce the same checksum as "brown The quick".