The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    20140508-Delphi-2007--Project-Options--Cannot-Edit-Application-Title-HelpFile-Icon-Theming

    20140430-Fiddler-Filter-Actions-Button-Run-Filterset-now

    20140424-Windows-7-free-disk-space

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,715 other followers

Archive for the ‘Database Development’ Category

Firebird – adjusting Delphi stored TDateTime (or OLE Automation date) fields that contain Dates, Times or DateTimes

Posted by jpluimers on 2017/02/15

Delphi programmers often store TDateTime (be it date, time or date+time values) as floating points in the database. They take the easy way out basically because TDateTime is nothing but a typed alias for the Delphi Double type (which is equivalent to the IEEE 754 double-precision binary floating-point format: binary64 type) .

Actually, TDateTime is equivalent to the OLE Automation date (which is described in the remarks of  DateTime.ToOADate Method (System)) which has an epoch of  midnight at 1899-12-30 (to be compatible with Lotus-123 including its leap-year-bug, background on that is at Why are the Microsoft Office file formats so complicated? (And some workarounds) – Joel on Software).

Anyway: the definition is this:

An OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899, and whose fractional component represents the time on that day divided by 24. For example, midnight, 31 December 1899 is represented by 1.0; 6 A.M., 1 January 1900 is represented by 2.25; midnight, 29 December 1899 is represented by -1.0; and 6 A.M., 29 December 1899 is represented by -1.25.

The base OLE Automation Date is midnight, 30 December 1899. The minimum OLE Automation date is midnight, 1 January 0100. The maximum OLE Automation Date is the same as DateTime.MaxValue, the last moment of 31 December 9999.

The sample values are remarkably the same as the ones for TDateTime:

Value Description
0 December 30, 1899; 12:00 A.M.
2.75 January 1, 1900; 6:00 P.M.
-1.25 December 29, 1899; 6:00 A.M.
35065 January 1, 1996; 12:00 A.M.

The below SQL example uses the Firebird date/time/timestamp casting shortcuts:

select date      '1899-12-30'              + 42452.670590278 as Date20160323,
       timestamp '1899-12-30 00:00:00.000' + 42452.670590278 as DateTime20160323160539,
       time       '00:00:00'                + 60*60*24 * 0.5 as Noon,
       timestamp '1899-12-30 00:00:00.000' + 0          as DateTime18991230Midnight,
       timestamp '1899-12-30 00:00:00.000' + 2.75           as DateTime190001011800,
       timestamp '1899-12-30 00:00:00.000' + -1.25          as DateTime189912290600,
       timestamp '1899-12-30 00:00:00.000' + 35065          as DateTime19960101Midnight
from   rdb$database

Which – using DMY date format and 24 hour clock format settings – results in:

DATE20160323 DATETIME20160323160539 NOON       DATETIME18991230MIDNIGHT DATETIME190001011800 DATETIME189912290600 DATETIME19960101MIDNIGHT CONSTANT
---------------------------------------------------------------------------------------------------------------------------------------------------
24-3-2016    23-3-2016 16:05:39     12:00:00   30-12-1899               1-1-1900 18:00:00    28-12-1899 18:00:00  1-1-1996

Which basically taught me a new thing about firebird: Times are calculated in seconds, so date fractions need to be multiplied by 60 * 60 * 24.

Note this post is complementary to Date format converter from Text or Unix/Mac/Filetime/Microsoft to virtually any readable form (which mentions the wrong Microsoft epoch as it should be 1899-12-30 midnight): the above shows how to do the conversion to readable dates in Firebird (might work in InterBase as well, but I’ve not used that for a long time).

–jeroen

PS: for Microsoft SQL Server: passing dates to the sql server: pass it as float – 2. -2 is the difference between delphi tdatetime start date and mssql start date.

 

Posted in Algorithms, Database Development, Development, Firebird, Floating point handling, Software Development | 2 Comments »

How to speed up Count(*) in Interbase/Firebird – Stack Overflow

Posted by jpluimers on 2017/02/14

The drawback of how the multigenerational architecture is implemented:

Even when an index is available on the column or columns included in the COUNT, all records must be visited in order to see if they are visible under the current transaction isolation.

The benefits of having far less locking than other architectures means you need to be careful using the COUNT operator on large result-sets.

In many systems the exact result of a COUNT isn’t very important, so you can use the workaround referred from How to speed up Count(*) in Interbase/Firebird – Stack Overflow

–jeroen

via: Select count(*) is slow

Posted in Database Development, Development, Firebird, InterBase | Leave a Comment »

Static Code Analyzer for T-SQL – MS SQL Server. Plugs into MS SSMS and can al…

Posted by jpluimers on 2017/01/12

Static Code Analyzer for T-SQL – MS SQL Server.

Plugs into MS SSMS and can also be run from command line.It reports useful clues which you can turn/on off to your liking. http://sqlcodeguard.com/index-database-issues.html

It will spot declared but unused variables, but it appears it doesn’t do code coverage or execution path to spot stuff like variables being used without being initialized.

http://sqlcodeguard.com/ Price: Free

Source: Lars Fosdal on G+: Static Code Analyzer for T-SQL – MS SQL Server. Plugs into MS SSMS and can al…

–jeroen

Posted in Database Development, Development, SQL, SQL Server, SQL Server 2005, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014 | Leave a Comment »

Firebird – date and time literals, conversion to ISO-8601 compatible strings

Posted by jpluimers on 2016/12/28

For my reference:

select 'Date literal values' as description
      , (cast('now' as timestamp)) as nowTimeStamp
      , (cast('today' as timestamp)) as todayTimeStamp
      , (cast('tomorrow' as timestamp)) as tomorrowTimeStamp
      , (cast('yesterday' as timestamp)) as yesterdayTimeStamp
      , cast(cast('Now' as date) as varchar(10)) as nowDateCast
      , replace(cast(cast('Now' as date) as varchar(10)), '-', '') nowDateIso8601
      , cast(cast('Now' as time) as varchar(13)) as nowTimeCast
      , replace(cast(cast('Now' as time) as varchar(13)), ':', '') nowTimeIso8601
      , cast(cast('Now' as timestamp) as varchar(24)) as nowTimeStampCast
      , substring(replace(replace(replace(cast(cast('Now' as timestamp) as varchar(24)), '-', '') , ' ', 'T'), ':', '') from 1 for 15) as nowTimeStampIso8601
from rdb$database 
;

Output and transposed output:

DESCRIPTION         NOWTIMESTAMP            TODAYTIMESTAMP          TOMORROWTIMESTAMP       YESTERDAYTIMESTAMP      NOWDATECAST NOWDATEISO8601 NOWTIMECAST   NOWTIMEISO8601 NOWTIMESTAMPCAST         NOWTIMESTAMPISO8601     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Date literal values 3-8-2016 8:45:12        3-8-2016                4-8-2016                2-8-2016                2016-08-03  20160803       08:45:12.3860 084512.3860    2016-08-03 08:45:12.3860 20160803T084512         

DESCRIPTION             Date literal values
NOWTIMESTAMP            2016-08-03 8:45:12
TODAYTIMESTAMP          2016-08-03
TOMORROWTIMESTAMP       2016-08-04
YESTERDAYTIMESTAMP      2016-08-02
NOWDATECAST             2016-08-03
NOWDATEISO8601          20160803
NOWTIMECAST             08:45:12.3860
NOWTIMEISO8601          084512.3860
NOWTIMESTAMPCAST        2016-08-03 08:45:12.3860
NOWTIMESTAMPISO8601     20160803T084512

References:

–jeroen

Posted in Database Development, Development, Firebird | Leave a Comment »

Firebird [# CORE-3558] server reports that DB file is used by another application on secondary attachment attempt through a symlink

Posted by jpluimers on 2016/12/15

It took quite a while to find out what happened, but this is the underlying issue: [# CORE-3558] FB server reports that DB file is used by another application on secondary attachment attempt through a symlink – Firebird RDBMS Issue Tracker.

The error you get in Windows is this one: I/O error during “CreateFile (open)” “The process cannot access the file because it is being used by another process.”

When you look with Process Explorer or handle.exe (both from SysInternals) they will show exactly the same filename (the canonical name to which the SymLink points).

But when you look in the config files of the applications, the database connections point to different filenames. Either (or both) are a symlink themselves or have a directory in their path that is a symlink.

 

In my case it was the latter: one of the directories in the path had an mklink.exe created link to another directory that had a directory contained the final file. There is even an API function that gets the canonical file name: GetFinalPathNameByHandle function (Windows)

Firebird doesn’t use that API on Windows (probably because it’s only available as of Windows Vista and Windows Server 2008) but does use a similar POSIX function (so on Mac OS X and Linux you don’t get this error).

Note that the above error messages can also happen when you use an embedded connection to a database (i.e. a connection string without a network address). There it is normal you get an exclusive file lock.

–jeroen

via: ADO.NET – a problem with multiple connections to the database (Russian) with Google Translate

Posted in Database Development, Development, Firebird, Software Development | Leave a Comment »

 
%d bloggers like this: