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.

Link to comment
Share on other sites

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 );

Link to comment
Share on other sites

  • 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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.