1

I want to create a ComparableExpression that will produce a datetime literal in the generated SQL, in the dialect of the configured DBMS. I have read this article: How to get fully materialized query from querydsl, which produces a literal for a Long value, but I haven't seen an example that produces a date or datetime. Date literal formatting is peculiar to different DBMSs.

So in the WHERE statement I want to get this:

dbo.fact_table.tx_date >= {formatted date literal}

For the {formatted date literal}, I want to generate:

'19931123 00:00:00' (for MSSQL) 

and

Timestamp '1993-11-23 00:00:00' (for Teradata)

The closest I have got is:

Expressions.dateTimeTemplate(Date.class, "{0}", alreadyFormattedDateString);

But then I have to format the date myself. I want to provide the Date object and get querydsl to do the formatting.

Jim Walmsley
  • 31
  • 1
  • 6

1 Answers1

1

My colleague has answered this for me.

We start with an OffsetDateTime but have to convert to java.util.Date because querydsl does not support java.time yet.

OffsetDateTime odt = OffsetDateTime.parse("2016-02-01T00:00:00+13:00");
Date date = Date.from(odt.toInstant());
Expression<Date> expr = Expressions.asDateTime(date);

The SQL literal produced for this Expression in MSSQL dialect is

{ts '2016-02-01 00:00:00'}

Thanks to Simon Lewis.

Jim Walmsley
  • 31
  • 1
  • 6