Jump to content

Derby/PostgreSQL/MySQL cross-db compatible way to extract date information?


Recommended Posts

Bleh this is driving me nuts, silly assignment where I have to use the 3 databases in the subjectline without a database abstraction layer.

Dates in particular are annoying.

EXTRACT( 'YEAR', date ) works with postgresql and mysql but not with derby

YEAR( date ) works with derby and mysql, but not with postgresql

to_char( 'yyyy', date ) only works with postgresql

 

So besides a regular expression to replace YEAR( DATE ) with EXTRACT( 'YEAR', date ).. any other options?

 

[edit]

Oh right, it's not just years, also months/days.

Okay deadline passed. Still interested if anyone knows a way though.

 

I ended up using YEAR( date ) etc as both Derby and MySQL understand this and postgresql will get the following from a function

$q = str_replace( "YEAR( ", "EXTRACT( YEAR FROM", $q );

  • 1 month later...

Just in case you are still interested. This will add the mysql style date() to postgres. Its better than doing string replaces.

 

CREATE OR REPLACE FUNCTION year(date) 
RETURNS int AS
$$
   SELECT EXTRACT(YEAR FROM $1)::int
$$ LANGUAGE 'sql' IMMUTABLE STRICT;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.