0
votes

When I connect to a Firebird database and try to execute a query, it throws me this error:

    "Error while preparing SQL statement:")
fdb.fbcore.DatabaseError: ('Error while preparing SQL statement:\n- SQLCODE: -104\n- Dynamic SQL Error\n- SQL error code = -104\n- Token unknown - line 27, column 113\n- )', -104, 335544569)

My query looks like:

select distinct p.ID, p.DATE, p.PETITION, pac.KEY, ..., 
  pac.BIRTH_DATE, trunc(('today' - pac.BIRTH_DATE)/365.25) as AGE, p.NAME,...

And I've checked that the cause of the error is:

trunc(('today' - pac.BIRTH_DATE)/365.25) as AGE

if I remove it from the query, it works perfectly. Anyone knows why? Could you help me?

I've done what @ain told me:

trunc((cast(CURRENT_DATE - pac.BIRTH_DATE) as DOUBLE PRECISION)/365.25) as AGE

    "Error while preparing SQL statement:")
fdb.fbcore.DatabaseError: ('Error while preparing SQL statement:\n- SQLCODE: -104\n- Dynamic SQL Error\n- SQL error code = -104\n- Database SQL dialect 1 does not support reference to DATE datatype', -104, 335544569)

But this results in the same error. Also if I try:

 DATEDIFF(YEAR from CURRENT_DATE to pac.BIRTH_DATE) as AGE

    "Error while preparing SQL statement:")
fdb.fbcore.DatabaseError: (
     'Error while preparing SQL statement:\n-
      SQLCODE: -104\n- 
      Dynamic SQL Error\n- 
      SQL error code = -104\n- 
      Database SQL dialect 1 does not support reference to DATE datatype',
   -104, 335544569)

Finally I solved the issue by doing:

... trunc(DATEDIFF(DAY from cast(pac.BIRTH_DATE as DATE) to CURRENT_TIMESTAMP)/365.25) as AGE ...

I'll take a look to dialects too. Thank you so much for your help!

2
Have you noticed that the error is now different: "Database SQL dialect 1 does not support reference to DATE datatype". Why do you use dialect 1 ? - ain
The last problem should be fixed by using CURRENT_TIMESTAMP instead of CURRENT_DATE, dialect 1 datatype DATE is actually a timestamp, so CURRENT_DATE doesn't work as it has an unknown datatype for dialect 1. - Mark Rotteveel
Hello @MarkRotteveel Thank you for your comment, it fixed the problem! - Solar

2 Answers

0
votes

You have to cast 'today' to date, otherwise it is just a string literal. Ie use

... trunc((cast('today' as date) - pac.BIRTH_DATE)/365.25) as AGE ...

You might also want to use CURRENT_DATE instead, see Context variables topic of the Firebird manual.

It seems you want to calculate age in "full years" so you could use DATEDIFF function for that:

DATEDIFF(YEAR from CURRENT_DATE to pac.BIRTH_DATE)
0
votes

You are not supposed to use Dialect 1. It was obsoleted with releases of Interbase 6 and Firebird 0.9 in 2000. It is sole intention is backward compatibility with pre-2000 database server versions for pre-2000 legacy applications. In 2018 this purpose has little practical value and hence Dialect 1 not only blocks you from using most recent language and engine enhancements, but maybe outright buggy due to little real world exposure thus little out of the house testing.

You have to migrate your database to Dialect 3. Checking and ironing out all the incompatibilities that the database creator maybe introduced to it, coding it by pre-2000 language, along your migration.