Gep
Gep

Reputation: 928

SQL Server built-in functions in H2

I'm working on a Spring Data JPA application that connects to SQL Server and uses H2 for integration testing (JUnit). I have a native SQL query that uses the SQL Server DATENAME function, like this:

CASE WHEN DATENAME(dw, datetime) = 'Sunday' THEN …

The issue is that DATENAME doesn’t exist in H2. H2 has similar functions like DAYNAME, but they are not available in SQL Server. This makes it difficult to write a SQL query that works for both the application and the unit tests.

Do you have any suggestions?

P.S. Here’s what I’ve tried so far:

  1. Created a custom function in H2 called DATENAME. This works if the first parameter is a string literal:

    DATENAME('dw', datetime)
    

    However, rewriting the SQL query this way causes it to stop working on SQL Server.

  2. Created a function in SQL Server called DAYNAME:

    CREATE FUNCTION DAYNAME(@dateValue DATETIME2)
        RETURNS NVARCHAR(30)
    AS
    BEGIN
        RETURN DATENAME(dw, @dateValue)
    END
    

    This approach seemed promising because H2 has a built-in DAYNAME function. However, it doesn’t work because in SQL Server, you need to prefix it with dbo, like this:

    dbo.DAYNAME(datetime)
    

    And this doesn’t work with H2.

Any ideas would be appreciated!

Thanks!

Upvotes: 1

Views: 75

Answers (0)

Related Questions