0

I have the following script that never retrieve any data since TimeStamp data type in Firebird/Interbase is not DateTime (*)

How should i fix it ?? TX.

 with DMApp.qryValidPeriod_ do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select P.* from PERIOD P, FISCAL_YR F');    
    SQL.Add('where');
    SQL.Add('P.FISCAL_YR_ID = F.FISCAL_YR_ID and');     
    SQL.Add('F.ENTITY_DB_ID = :PRIMARY_DB_ID and');     
    SQL.Add('F.FISCAL_YR_ID = :CURR_FY_ID and');        
    SQL.Add(':pTranDate BETWEEN P.BEG_PERIOD and P.END_PERIOD');

    ParamByName('pTranDate').AsDateTime :=  sBATCH_DATE; 

// BEG_PERIOD and END_PERIOD are TimeStamp data type such as '2010-11-09 12:00' // parameter return data type such as '2010-11-09'


(*) A timestamp is a column datatype available in Firebird/Interbase/SQL Server... that helps to ensure data integrity. Timestamps are automatically updated every time a row containing a timestamp column is inserted or updated. Values in timestamp columns are not datetime data, but binary(8) varbinary(8) data. The timestamp datatype has no relation to the system time, it is simply a monotonically increasing counter whose values will always be unique within a database (making it basically a unique random number.)

6
  • 1
    Is TimeStamp in this example your own custom datatype? Because we work a lot with standart TIMESTAMP datatype and it works fine when compared to query parameters. Nov 11, 2010 at 6:44
  • No it is not. It is Firebird 2.5. As shown above, the scrip never return any data - (because the value of the parameter pTranDate is of date format only as seen in the debugger)
    – volvox
    Nov 11, 2010 at 13:10
  • 1
    That code should normally work. The reason why the query returns no results may be something different. Check your other parameters. Nov 11, 2010 at 13:12
  • Exemple: Delphi put '2010-11-09' as the value of the pTranDate parameter. With this value the script returns no data. If i literally replace this parameter value with '2010-11-09 00:00' then the script returns data.
    – volvox
    Nov 11, 2010 at 13:24
  • '2010-11-09' is a string value, how do you set it to the parameter? Nov 11, 2010 at 15:28

3 Answers 3

5

Isn't that you misunderstood TIMESTAMP datatypes in Firebird? In SQL92 TIMESTAMP is a standard data type to hold both date and time values. It's not automatically updated and the functions returning TIMESTAMP values are related to the database time.

AFAIK, the Firebird TIMESTAMP implementation follows the SQL 92 rules, and it should work perfectly with a Delphi .AsDateTime parameter.

It's SQL Server TIMESTAMPs that are as you described - and should never be used in comparison with date and times, "The SQL Server timestamp data type has nothing to do with times or dates" http://msdn.microsoft.com/en-us/library/ms191240(SQL.90).aspx

2
  • Sorry - yes i was describing SQL Server TIMESTAMP. My above script return data only when param pTranDate is of 'Date''Time' format (that is 2010-11-09 00:00). As it is i only get the Date (2010-11-09)
    – volvox
    Nov 11, 2010 at 13:07
  • SORRY guys - i was mixing up things between Firebird and SQL Server (because my application is multi server). NOW with your help i got things right and Delphi DateTime is working OK with Firebird TimeStamp data type.
    – volvox
    Nov 11, 2010 at 14:23
1

An UDF below written in Delphi. It gets three parts of a date and returns date value in Firebird format. Maybe this will help you to understand how to convert dates between Delphi and FB.

type
  PIBDateTime = ^TIBDateTime;
  TIBDateTime = record
    Days,                           // Date: Days since 17 November 1858
    MSec10 : Integer;               // Time: Millisecond * 10 since midnigth
  end;

const                               // Date translation constants
  MSecsPerDay10 = MSecsPerDay * 10; // Milliseconds per day * 10
  IBDateDelta = 15018;              // Days between Delphi and InterBase dates

// ==============================================
//  declare external function ...
//    smallint, smallint, smallint, date
//  returns
//    date
//  ...
// ==============================================

function g_d_encodedate(var Year, Month, Day: SmallInt;
  var IBDateTime: TIBDateTime): PIBDateTime; cdecl; export;
var
  DateTime: TDateTime;
  DelphiDays : Integer;
begin
  DateTime := EncodeDate(Year, Month, Day);
  DelphiDays := Trunc(DateTime);
  with IBDateTime do begin
    Days := DelphiDays + IBDateDelta;
    MSec10 := Trunc((DateTime - DelphiDays) * MSecsPerDay10);
  end;
  Result := @IBDateTime;
end;
1

Usually, I use it as string parameter, because of the time part. Idea is to have settings independent format that is for example '11-NOV-2010 12:32:25'

For example, you might try

ParamByName('pTranDate').AsString :=  FormatSQLDateTime(sBATCH_DATE);

Where as you should have functions similar to these

function FormatSQLDate(date: TDate) : string;
const months : array[1..12] of string[3] =
('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
 var d, y : integer;
     m : string[3];
begin
   d := DayOf(date);
   y := YearOf(date);
   m := months[MonthOf(date)];
   Result := Format('%d-%s-%d', [d, m, y]);
end;


function FormatSQLDateTime(date : TDateTime) : String;
var
  d : String;
  h, m, s : Integer;
begin
  d := SQLDate(date);
  h := HourOf(date);
  m := MinuteOf(date);
  s := SecondOf(date);
  Result := d + Format(' %d:%d:%d', [h, m, s]);
end;

Notice that you can also work something out on the server side with use of sql extract

extract(day from timestamp_field)

See the documentation of EXTRACT.

2
  • 1
    Usually is better to keep date in numeric formats and let the driver/client/database take care of the rest. It is also slightly faster because the engine doesn't need to parse and convert the string. Firebird has less issues because it understands a fixed set of string literals, but in other databases the "standard" date literals format can change depending on the OS locale, database settings and client locale and settings. IMHO it's a good prarctice to avoid strings.
    – user160694
    Nov 11, 2010 at 11:55
  • Thanks for this function and reference Sofija - it will be helpfull in some other part of my application. Now i got things right using Firebird TimeStamp and Delphi DateTime as it is.
    – volvox
    Nov 11, 2010 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.