Where clause dynamic postgresql with date

Highlighted
Six Stars

Where clause dynamic postgresql with date

Hi all, I'm trying to get a date from a database select, save it in a global variable and reuse the same variable to put it in a where clause. All inputs are postgresql database.
This is the select in object:
SELECT
  "Postgres". "Public." "Importconfig." "Tables",
  "Postgres". "Public." "Importconfig." "Datavalue"
FROM "postgres". "Public". "Importconfig"
where "postgres". "public". "importconfig". "datavalue"> '"+ (Date) globalMap.get (" MaxDate ")) +"'
and under the scheme I use.
This is the error:
Exception in component tDBInput_2 (test)
org.postgresql.util.PSQLException: ERROR: input syntax not valid for date type: "" + (Date) globalMap.get ("MaxDate")) + ""
  Position: 203
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2440)
at org.postgresql.core.v3.QueryExecutorImpl.processResults (QueryExecutorImpl.java:2183)
at org.postgresql.core.v3.QueryExecutorImpl.execute (QueryExecutorImpl.java308)
at org.postgresql.jdbc.PgStatement.executeInternal (PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute (PgStatement.java:365)
at org.postgresql.jdbc.PgStatement.executeWithFlags (PgStatement.java307)
at org.postgresql.jdbc.PgStatement.executeCachedSql (PgStatement.java:293)
at org.postgresql.jdbc.PgStatement.executeWithFlags (PgStatement.java:270)
at org.postgresql.jdbc.PgStatement.executeQuery (PgStatement.java:224)
at local_project.test_0_1.test.tDBInput_2Process (test.java:1813)
at local_project.test_0_1.test.tDBInput_1Process (test.java:1201)
at local_project.test_0_1.test.runJobInTOS (test.java:2326)
at local_project.test_0_1.test.main (test.java:2171)

 

thank's

Cattura.PNG


Accepted Solutions
Employee

Re: Where clause dynamic postgresql with date

Hi,

 

    You should use the PostgreSQL to_date function to convert the input string value instead of doing a java date conversion. The SQL query is first validated for correct syntax at java level and then at SQL level. Since you are adding a date directly to another string, its giving data type error.

eg:-   

 

SELECT to_date('20170103','YYYYMMDD');

 

 

In you case, the most easy way is to assign the maxdate to a context variable in string data type where value is in YYYYMMDD format.

 

 

"SELECT
  Tables,
  Datavalue
FROM postgres.Public.Importconfig
where datavalue> to_date('"+context.max_date+"','YYYYMMDD')"

 

 

Warm Regards,
Nikhil Thampi

Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved :-)

 


All Replies
Employee

Re: Where clause dynamic postgresql with date

Hi,

 

    You should use the PostgreSQL to_date function to convert the input string value instead of doing a java date conversion. The SQL query is first validated for correct syntax at java level and then at SQL level. Since you are adding a date directly to another string, its giving data type error.

eg:-   

 

SELECT to_date('20170103','YYYYMMDD');

 

 

In you case, the most easy way is to assign the maxdate to a context variable in string data type where value is in YYYYMMDD format.

 

 

"SELECT
  Tables,
  Datavalue
FROM postgres.Public.Importconfig
where datavalue> to_date('"+context.max_date+"','YYYYMMDD')"

 

 

Warm Regards,
Nikhil Thampi

Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved :-)

 

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Why Companies Move to the Cloud: 7 Success Stories

Learn how and why companies are moving to the Cloud

Read Now