Solved

Compute Date and Time with SQL

Posted on 2003-11-21
20
406 Views
Last Modified: 2010-04-05
How will i get the difference between the two Date/time fields using sql?

fields are:

BadgeNo, DateIn, TimeIn, DateOut, TimeOut

example (Inside the Database)
"EMPLOYEE.DB"
badgeNo    fullName
    01         Mark oNe
    02         Edwin Two
    03         Sofia Three

"DTR.DB"
badgeNo    DateIn           TimeIn               DateOut             TimeOut
     01        11/21/2003    08:00:00 am      11/21/2003        09:00:00 am
     02        11/21/2003    08:00:00 am      11/21/2003        10:00:00 am
     01        11/21/2003    08:00:00 am      11/21/2003        11:00:00 am
     03        11/21/2003    08:00:00 am      11/21/2003        09:00:00 am


WHEN displayed in my Grid

badgeNo    FullName         Total_Time
   01          Mark oNe                4
   02          Edwin Two              2
   03          Sofia Three             1


Note:
 formula must be  (DateOut+timeOut) - (DateIn + TimeIn)
example
 ( 11/21/2003+09:00:00 am ) - (11/21/2003  +  08:00:00 am)  
answer is
  1 hr.

 Anybody can help?




0
Comment
Question by:girlswants_me
20 Comments
 

Author Comment

by:girlswants_me
ID: 9795450
anybody?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9795675
what for types are these fields (DateIn, TimeIn, DateOut, TimeOut)?
Date, time, datetime, char, varchar?
what database?
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9795796
Depends on the database server that you're using. This is not a Delphi issue but a database issue. And not all databases will allow calculations with date fields.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9796266
The database is Paradox I think ("EMPLOYEE.DB") and BDE is being used perhaps.

Now the question that remains is: what are the types of the fields.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9796966
Assuming your DateX fields are of DATE type and your TimeX fields are of TIME type then your SQL may look like (Paradox):

SELECT B.badgeNo, A.fullName,
SUM(CAST((
(B.DateOut - B.DateIn) * 24
+
(B.TimeOut - B.TimeIn) * 24
) AS INTEGER))
 AS B.Total_Time FROM "EMPLOYEE.DB" AS A, "DTR.db" AS B
WHERE A.badgeNo = B.badgeNo
GROUP BY B.badgeNo, A.fullName
ORDER BY B.badgeNo

Regards, Geo
0
 

Author Comment

by:girlswants_me
ID: 9807857
Hi everyone,

I used this to create the database

procedure TDataModule1.CreateTable_OTDTR(NameFile: string; databasePath:string);
var
 DatabaseDirectory:string;
begin
  // Create Database
  with TTable.Create(Application) do
  begin
    Active := False;
    DatabaseName := databasePath;
    TableName := NameFile;
    TableType := ttDBase;
    with FieldDefs do
    begin
      Clear;
      Add('IndexNo'        , ftString         , 15   , false);
      Add('BadgeNo'        , ftString         ,  6   , false);
      Add('ShiftCode'      , ftString         ,  5   , false);
      Add('MealRate'       , ftFloat          ,  0   , false);
      Add('DateIn'         , ftDate           ,  0   , false);
      Add('TimeIn'         , ftTime           ,  0   , false);
      Add('DateOut'        , ftDate           ,  0   , false);
      Add('TimeOut'        , ftTime           ,  0   , false);
      Add('SchedCode'      , ftString         ,  4   , false);
    end;
    CreateTable;
    with IndexDefs do
    begin
      Clear;
      Add('IndexNo'        , 'IndexNo'        , []);
    end;
    CreateTable;
    Free;
  end;
end;
0
 

Author Comment

by:girlswants_me
ID: 9807946
This cause a Type mismatch error

 wwQuery1.SQL.Add('SELECT B.BadgeNo, A.EmpName,SUM((((B.DateOut - B.DateIn) * 24)+((B.TimeOut - B.TimeIn) * 24))) AS B.Total_Time FROM '+main.DB_Employee+' AS A, '+main.DB_OTDTR+' AS B');
 wwQuery1.SQL.Add('WHERE A.BadgeNo = B.BadgeNo');
 wwQuery1.SQL.Add('GROUP BY B.BadgeNo, A.EmpName, B.DateOut');
 wwQuery1.SQL.Add('ORDER BY B.BadgeNo');
 wwQuery1.ExecSQL;
 wwQuery1.Open;
 Browse.DataSource:=wwDataSource2;
0
 

Author Comment

by:girlswants_me
ID: 9807950
My database is not a paradox.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9809305
Please, follow the exact SQL statement I've provided:
- I used CAST AS INTEGER for the SUM and you didn't.
- Also B.DateOut cannot be in the GROUP BY because you don't have such a column in your SELECT list.
- Remove wwQuery1.ExecSQL; - this is wrong !
- Browse.DataSource:=wwDataSource2; should be before wwQuery1.Open; if this is something related to the query.

Regards, Geo
0
 

Author Comment

by:girlswants_me
ID: 9828814
The first source code you've posted was first applied but it failed on that "CAST" part.
That is why i removed that area.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:girlswants_me
ID: 9830192
wwQuery1.DatabaseName:=main.Database_Path;
 wwQuery1.SQL.Clear;

 wwQuery1.SQL.Add('SELECT B.BadgeNo, A.EmpName,');
 wwQuery1.SQL.Add('SUM(CAST((');
 wwQuery1.SQL.Add('(B.DateOut - B.DateIn) * 24');
 wwQuery1.SQL.Add('+');
 wwQuery1.SQL.Add('(B.TimeOut - B.TimeIn) * 24');
 wwQuery1.SQL.Add(') AS INTEGER))');
 wwQuery1.SQL.Add(' AS B.Total_Time FROM "'+main.DB_Employee+'" AS A, "'+main.DB_OTDTR+'" AS B');
 wwQuery1.SQL.Add('WHERE A.BadgeNo = B.BadgeNo');
 wwQuery1.SQL.Add('GROUP BY B.BadgeNo, A.EmpName');
 wwQuery1.SQL.Add('ORDER BY B.BadgeNo');
 wwQuery1.Open;
 Browse.DataSource:=wwDataSource2;

STILL HAVING PROBLEM.
"Type Mismatch in expression." <--- error
0
 
LVL 17

Expert Comment

by:geobul
ID: 9831242
Your database is not Paradox. OK. It's DBASE then, I guess. Is that right? Please, tell me what it is (not what it isn't). I'm confused because you're naming your tables .DB (paradox) but setting TableType to ttDBase later on.

If your db is DBASE then ftTime fields are CHAR(11) actually not TIME (as they would be in Paradox). That's why you can't substract them directly. You should use:

(CAST(B.TimeOut AS TIME) - CAST(B.TimeIn AS TIME)) * 24

instead of

(B.TimeOut - B.TimeIn) * 24

That's all.

Regards, Geo
0
 

Author Comment

by:girlswants_me
ID: 9834944
wwQuery1.DatabaseName:=main.Database_Path;
 wwQuery1.SQL.Clear;

 wwQuery1.SQL.Add('SELECT B.BadgeNo, A.EmpName,');
 wwQuery1.SQL.Add('SUM(CAST((');
 wwQuery1.SQL.Add('(CAST(B.DateOut AS TIME) - CAST(B.DateIn AS TIME)) * 24');
 wwQuery1.SQL.Add('+');
 wwQuery1.SQL.Add('(CAST(B.TimeOut AS TIME) - CAST(B.TimeIn AS TIME)) * 24');
 wwQuery1.SQL.Add(') AS INTEGER))');
 wwQuery1.SQL.Add(' AS B.Total_Time FROM "'+main.DB_Employee+'" AS A, "'+main.DB_OTDTR+'" AS B');
 wwQuery1.SQL.Add('WHERE A.BadgeNo = B.BadgeNo');
 wwQuery1.SQL.Add('GROUP BY B.BadgeNo, A.EmpName');
 wwQuery1.SQL.Add('ORDER BY B.BadgeNo');
 wwQuery1.Open;

I already used the "CAST" but
STILL HAVING PROBLEM.
"Type Mismatch in expression." <--- error
0
 
LVL 17

Expert Comment

by:geobul
ID: 9835812
I'm sorry to hear that. Both versions (paradox and dbase) work just fine here.
0
 

Author Comment

by:girlswants_me
ID: 9836234
Here is the complete code that i used. Hope this will help, or maybe i have a problem with my query table used.

I used TQuery.


procedure TSubsidy.Button2Click(Sender: TObject);
begin
 wwQuery1.DatabaseName:=main.Database_Path;
 wwQuery1.SQL.Clear;

 wwQuery1.SQL.Add('SELECT B.BadgeNo, A.EmpName,');
 wwQuery1.SQL.Add('SUM(((');
 wwQuery1.SQL.Add('((B.DateOut) - (B.DateIn)) * 24');
 wwQuery1.SQL.Add('+');
 wwQuery1.SQL.Add('((B.TimeOut) - (B.TimeIn)) * 24');
 wwQuery1.SQL.Add(')))');
 wwQuery1.SQL.Add('AS B.Total_Time FROM "'+main.DB_Employee+'" AS A, "'+main.DB_OTDTR+'" AS B');
 wwQuery1.SQL.Add('WHERE A.BadgeNo = B.BadgeNo');
 wwQuery1.SQL.Add('GROUP BY B.BadgeNo, A.EmpName');
 wwQuery1.SQL.Add('ORDER BY B.BadgeNo');
 wwQuery1.Open;
 Browse.DataSource:=wwDataSource2;
end;


in my datamodule

procedure TDataModule1.CreateTable_OTDTR(NameFile: string; databasePath:string);
var
 DatabaseDirectory:string;
begin
  // Create Database
  with TTable.Create(Application) do
  begin
    Active := False;
    DatabaseName := databasePath;
    TableName := NameFile;
    TableType := ttDBase;
    with FieldDefs do
    begin
      Clear;
      Add('IndexNo'        , ftString         , 15   , false);
      Add('BadgeNo'        , ftString         ,  6   , false);
      Add('ShiftCode'      , ftString         ,  5   , false);
      Add('MealRate'       , ftFloat          ,  0   , false);
      Add('DateIn'         , ftDate           ,  0   , false);
      Add('TimeIn'         , ftFloat          ,  0   , false);
      Add('DateOut'        , ftDate           ,  0   , false);
      Add('TimeOut'        , ftFloat          ,  0   , false);
      Add('SchedCode'      , ftString         ,  4   , false);
    end;
    CreateTable;
    with IndexDefs do
    begin
      Clear;
      Add('IndexNo'        , 'IndexNo'        , []);
    end;
    CreateTable;
    Free;
  end;
end;
0
 
LVL 17

Expert Comment

by:geobul
ID: 9836362
Wow, time fields have become of float type now! Let it be...

Try changing:
wwQuery1.SQL.Add('((B.TimeOut) - (B.TimeIn)) * 24');
to
wwQuery1.SQL.Add('CAST(((B.TimeOut) - (B.TimeIn)) * 24) AS INTEGER');

Regards, Geo
0
 

Author Comment

by:girlswants_me
ID: 9836426
i have received "Invalid use of token ")""

Please see this code. I wonder whats wrong with this.

 wwQuery1.DatabaseName:=main.Database_Path;
 wwQuery1.SQL.Clear;

 wwQuery1.SQL.Add('SELECT B.BadgeNo, A.EmpName,');
 wwQuery1.SQL.Add('SUM(((');
 wwQuery1.SQL.Add('((B.TimeOut) - (B.TimeIn)) * 24');
 wwQuery1.SQL.Add('+');
 wwQuery1.SQL.Add('CAST(((B.TimeOut) - (B.TimeIn)) * 24) AS INTEGER');
 wwQuery1.SQL.Add(')))');
 wwQuery1.SQL.Add('AS B.Total_Time FROM "'+main.DB_Employee+'" AS A, "'+main.DB_OTDTR+'" AS B');
 wwQuery1.SQL.Add('WHERE A.BadgeNo = B.BadgeNo');
 wwQuery1.SQL.Add('GROUP BY B.BadgeNo, A.EmpName');
 wwQuery1.SQL.Add('ORDER BY B.BadgeNo');
 wwQuery1.Open;
 Browse.DataSource:=wwDataSource2;
0
 
LVL 17

Accepted Solution

by:
geobul earned 200 total points
ID: 9836489
wwQuery1.SQL.Add('SELECT B.BadgeNo, A.EmpName,');
 wwQuery1.SQL.Add('SUM(CAST((');
 wwQuery1.SQL.Add('(B.TimeOut - B.TimeIn) * 24');
 wwQuery1.SQL.Add('+');
 wwQuery1.SQL.Add('CAST(((B.TimeOut - B.TimeIn) * 24) AS INTEGER)');
 wwQuery1.SQL.Add(') AS INTEGER))');
 wwQuery1.SQL.Add('AS B.Total_Time FROM "'+main.DB_Employee+'" AS A, "'+main.DB_OTDTR+'" AS B');
 wwQuery1.SQL.Add('WHERE A.BadgeNo = B.BadgeNo');
 wwQuery1.SQL.Add('GROUP BY B.BadgeNo, A.EmpName');
 wwQuery1.SQL.Add('ORDER BY B.BadgeNo');
0
 

Author Comment

by:girlswants_me
ID: 9837089
Thank you very much geobul, For you price, i've raised the points to 200.
Youre the Master of Delphi
0
 
LVL 17

Expert Comment

by:geobul
ID: 9837198
Thank you very much :-)

BTW the third line in my last comment above is wrong. It should be:
wwQuery1.SQL.Add('(B.DateOut - B.DateIn) * 24');

Regards, Geo
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now