Search code examples
unit-testingsybaseh2

implementing the sybase datepart in h2


I'm working on testing a application that uses sybase ASE as it's database.

Our code uses the datepart function in sybase, for example like this:

select datepart(month, getdate());

I tried to implement this using an user defined function ( http://www.h2database.com/html/features.html#user_defined_functions ) like this:

public class SybaseSupport {
    public static String datePart(String input1, String input2) {
        return "";
    }
}

CREATE ALIAS DATEPART FOR "no.di.util.SybaseSupport.datePart";

But the problem is that the first argument to the datepart is just the plain word month, without any form of quotation, so H2 thinks that I'm trying to access some sort of column.

Is it possible to implement this somehow using a user defined function in H2 or do I need to patch H2 itself?


Solution

  • The word 'month', when occurring (without quotes) as the first argument in datepart() is recognized as valid ASE syntax (and there's a range of similar words and abbreviations for which the same holds). Obviously, this is an ASE-specific non-ANSI construct and you cannot expect to have a different database behave the same way. You'll need to figure out equivalent syntax in your target database.