2

I’ve a stored procedure that will get a person’s last name and insert it under Alias after she updated her last name on a form. But my boss wants to keep all previous last names whenever a person changes her name each time. I’ve the following data in the SQL table.

+----------------+---------------+----------------+------------------+
     PeopleID         FirstName       LastName            Alias
+-----------------+--------------+----------------+------------------+
     002112             Judy            Smith              Hall

Let’s say, Judy changes her last name again to Kingsley. Is it possible to grab her previous last name Smith and insert it behind Hall under Alias? I’m thinking maybe it could look like this table but have no idea how.

+----------------+---------------+----------------+------------------+
     PeopleID         FirstName       LastName            Alias
+-----------------+--------------+----------------+------------------+
     002112             Judy          Kingsley         Hall, Smith
Cezar
  • 55,636
  • 19
  • 86
  • 87
Esther
  • 69
  • 1
  • 2
  • 13
  • 1
    What SQL server are you working with? PostgreSQL, MySQL, or something else? – Anthony Atkinson Jul 11 '13 at 18:14
  • @AnthonyAtkinson: I'm using SQL Server 2008. – Esther Jul 11 '13 at 18:15
  • 2
    If you're wanting to use normalization, you could make a completely new table called Alias and have two columns: PeopleID and Alias - and I think you can see where I'm going with this. This would allow you to have as many aliases as you need without modifying your original table. I didn't post as an answer because I'm not really an expert at normalization so I can't help much further than this description. Best of luck. – Ricky Mutschlechner Jul 11 '13 at 18:20
  • 2
    [Is storing a delimited list in a database column really that bad?](http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad/3653574#3653574) – Bill Karwin Jul 11 '13 at 18:27
  • @RickyMutschlechner: Thanks Ricky, I got what you mean. – Esther Jul 11 '13 at 19:03

2 Answers2

2

I suggest you to hold the current value in your table and create an history table to hold previous last name. Something like this :

CREATE TABLE last_name_history
(
   PeopleID char(6),
   from_date datetime,
   to_date datetime,
   alias char(25)
) 
Fabien TheSolution
  • 5,055
  • 1
  • 18
  • 30
-1

Retrieve all the values in a String array. Then keep all the alias in one string separated by commas. Let us say "alias" is a String array in which different alias are stored. Now, do something like this to store it in one string-

for(int i=0;i<alias.length;i++)
    {
                String str="";
        if(i==0)
    str+=alias[i];
        else
            str+=","+alias[i];
    }

Then store the string "str" in your column "Alias".

Naddy
  • 2,664
  • 6
  • 25
  • 38