Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Compute Date and Time with SQL

Posted on 2003-11-21
20
Medium Priority
?
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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 800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

705 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