Where clause dynamic postgresql with date

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

Tags (1)

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 :-)

 


Warm Regards,
Nikhil Thampi
Please appreciate our members by giving Kudos for spending 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 :-)

 


Warm Regards,
Nikhil Thampi
Please appreciate our members by giving Kudos for spending their time for your query. If your query is answered, please mark the topic as resolved :-)

What’s New for Talend Spring ’19

Watch the recorded webinar!

Watch Now

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.

Download

Tutorial

Introduction to Talend Open Studio for Data Integration.

Watch

Downloads and Trials

Test drive Talend's enterprise products.

Downloads