Axeia Posted May 27, 2009 Share Posted May 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159825-derbypostgresqlmysql-cross-db-compatible-way-to-extract-date-information/ Share on other sites More sharing options...
Axeia Posted May 29, 2009 Author Share Posted May 29, 2009 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 ); Quote Link to comment https://forums.phpfreaks.com/topic/159825-derbypostgresqlmysql-cross-db-compatible-way-to-extract-date-information/#findComment-844755 Share on other sites More sharing options...
artacus Posted July 1, 2009 Share Posted July 1, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/159825-derbypostgresqlmysql-cross-db-compatible-way-to-extract-date-information/#findComment-866963 Share on other sites More sharing options...
Axeia Posted July 1, 2009 Author Share Posted July 1, 2009 That's nifty, is that a one time per connection thing? (Can't really test atm as I don't have postgresql running) Quote Link to comment https://forums.phpfreaks.com/topic/159825-derbypostgresqlmysql-cross-db-compatible-way-to-extract-date-information/#findComment-867402 Share on other sites More sharing options...
artacus Posted July 2, 2009 Share Posted July 2, 2009 No, its a one time per database thing. It will create the function on your db and you'd call it just like you do the built in functions in mysql or derby. Quote Link to comment https://forums.phpfreaks.com/topic/159825-derbypostgresqlmysql-cross-db-compatible-way-to-extract-date-information/#findComment-867473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.