Urgent - Getting Unique Values from 5 Fields


The short questions is, is there an easy way to extract the unique dates
from 5 different fields in the same record?  Here's the long version - I
hope I can articulate this so that it's easy to understand.  While importing
records from an large CSV file (called TempDB), I check each field against
existing data integrity rules before I attempt to import that record.  Once
a record is deemed 'okay', I break it up into up to three different tables:

MASTER  Applicants - (SSN is link)
  Child 1        TestData
  Child 2        SpecialNeeds

The TempDB table contains information that will be broken out into the above
three tables.  In case it isn't obvious, students (Applicants table) can
take one or more tests (TestData table), therefore, a separate record needs
to be created in TestData for each 'unique' test date.  For example, if
TempDB has these dates in the record:

DATE1            DATE2        DATE3        DATE4            DATE5
01/01/2000    12/3/1999    01/01/2000    09/27/1999    12/03/1999

In the above example, I need to create '3' new records in the TestData table
to account for the unique test dates.  Since all of the student's
information is flattened (e.g., all part of a single record) in TempDB, I
cannot perform a loop.  There will never be more that 5 test dates; however,
there may be situations where less than 5 dates exist.

Is there an easy way to extract the unique dates from 5 different fields in
the same record?  PLEASE - I need code examples.  If I had time to do research, I wouldn't have posted the question here.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

kretzschmarConnect With a Mentor Commented:
well d4jaj1,

if ssDate is a typ of TDate or TDatetime,
then there must be a init.
ssdate := 0;  //somewhere in 1899
to add this type use
if ssDate <> 0 then then

if ssDate is a typ of String,
ssdate := '';  //somewhere in 1899
to add this type use
if ssDate <> '' then then

hope this helps

what database, d4jaj?
do you mean unique per tmpdb-record?
do you mean unique per tmpdb-record and ..?
is the tmpdb a table or just a flatfile?
is it a way to perform a tmptmpdb (a query-resultset)?
d4jaj1Author Commented:
The database part isn't really important, since at that time, it already open and I have access to the fields.  The question reads "There are 5 fields in teh TempDB tables with dates in them"  How can I compare all five fields and extract the unique dates (see grid above).  If u still think the tables matters, it's an Access 2000 DB via ODBC.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

hmm, i guess i've a slow thinking ;-)

maybe you can do it with a tsstringlist like

(stringlist allways created named sl like
  sl := TstringList.Create;
  sl.Duplicates := DupIgnore;
  sl.Sorted := True;

now you can use this code for adding

now you know with sl.count the records you must create
and the dates by iterating through the sl.

maybe you have to check if a date is empty like

if not(TempDB.FieldByName('Date1').IsNull) then

does this help?

d4jaj1Author Commented:
Yes it does, but I haven't gotten to the actual code yet.  I do try to check for nulls, but the dates are assigned to varibles instead of continually accessing the dbfield.

So, when I try this if

ssdate <> null then sl.Add(datetostr(ssdate));

The code doesn't work coorectly - meaning it tries to add ssdate and the stringlist count is 1 - even though all of the values in the five fileds are null.  I suspect that if I did this,

if not(TempDB.FieldByName('Date1').IsNull) then

It would work fine, but I need to keep the variables for other reasons.  I try

ssdate := null and/or nil

at teh beginning of the procedure in case it's never assigned, but neither work.  Nil won't compile and
null causes a run-time variant error.  ANy thoughts?

try this code :

procedure TForm1.Button1Click(Sender: TObject);
 I,J,C: Integer ;
 Dates : Array [1..5] of String ;
 DateFound : Boolean ;
 s: String ;
 Table1.Open ;
 while not Table1.EOF do
   for I:=1 to 5 do Dates[1]:='' ;
   Dates[1]:= Table1.FieldByName('Date1').AsString ;
   C:=1 ;
   For J:=2 to 5 do
    DateFound:= False ;
    for I :=1 to C do
       if Table1.FieldByName('Date'+IntToStr(J)).AsString=Dates[I] then
         DateFound:=True ;
         Break ;
       end ;
    if not DateFound then
      inc(C) ;
      Dates[C]:=Table1.FieldByName('Date'+IntToStr(J)).AsString ;
    end ;
   S:=EmptyStr ;
   For I:=1 to C do if trim(Dates[I])<>'' then addDateRecord ;
   Table1.Next ;
 end ;
select DISTINCT TD from
select  DISTINCT DATE1 as TD from TempDB
union (select  DISTINCT DATE2 as TD from TempDB)
union (select  DISTINCT DATE3 as TD from TempDB)
union (select  DISTINCT DATE4 as TD from TempDB)
union (select  DISTINCT DATE5 as TD from TempDB)
d4jaj1Author Commented:
kretzschmar ,

Thanks for the answer, the stringlist method works.  The only problem is the Date (ssDate).  I have to use a data variable because the dates in the TempDB file are broken into several fields, e.g.,

Month   Day1    Day2     Year
10       1       4        99

Unfortunately, I cannot perform an isnull because there are 4 fields to combine into a 'real' date - which is where ssDate comes from.

Now, I set all the date variables to 0 at the beginning of the code and the stringlist code works fine.  Unfortunately, I can't set ssDate back to nothing or blank.  Later in my import routine, is use

InsertRecord([keyfield, ssdate, wrdate, etc.])

to perform the insert.  Since none of the date variables are null/blank, 1899 is inserted into the table.  I know I can use NULL within InsertRecord to signify a blank field, but it doesn't allow me to test for zero 'within' the insertrecord statement.

Isn't their a way to truly convert the date variable to 'nothing', as if it was never initialized in the first place - sort of like a reset?
hi d4jaj1,

hmm, the TDateTime-Type is a Double-Type,
therefore it is not possible to null or nil it.

but you can ask for and decide like
if ssDate = 0 then
  InsertRecord([keyfield, NULL, wrdate, etc.])
  InsertRecord([keyfield, ssdate, wrdate, etc.]);

hope thats possible for you

d4jaj1Author Commented:
Nope, not possible because there are 5 date variables.  Oh well, guess I need to think of a better way of doing this.

You have answered the question I asked, so thank you for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.