I'm using SQL Server 2014 Developer Edition Service Pack 2 on Windows 7 Enterprise machine.
The question
Is there a set based way that I can create an integer field based on a string input? It must ensure that the Entity ID
field is never duplicated for different inputs?
Hypothetical table structure
|ID|Entity ID|Entity Code|Field One|From Date|To Date |
|1 |1 |CodeOne |ValueOne |20160731 |20160801|
|2 |1 |CodeOne |ValueTwo |20160802 |NULL |
|3 |2 |CodeTwo |ValueSix |20160630 |NULL |
Given the above table, I'm trying to find a way to create the Entity ID
based on the Entity Code
field (it is possible that we would use a combination of fields)
What I've tried so far
- Using a sequence object (don't like this because it is too easy for the sequence to be dropped and reset the count)
- Creating a table to track the Entities, creating a new
Entity ID
each time a new Entity is discovered (don't like this because it is not a set based operation) - Creating a hashbyte on the
Entity Code
field and converting this to aBIGINT
(I have no proof that this won't work but it doesn't feel like this is a robust solution)
Thanks in advance all.