ERROR: invalid input syntax for type timestamp: - postgresql

I am getting an Error in my PostgreSQL query as
ERROR: invalid input syntax for type timestamp:
I am setting the value using \set.
\set dueDateEarliest '2018-04-01'
\set dueDateLatest '2018-08-01'
\set dueDateLatest '2018-08-01'
And trying to use these value in my query as below
SELECT DISTINCT(bu.id) as "user_id",c.organization_name as "name",round(i.balance,2) as "amount_due",i.id as "invoice_number",i.due_date as "due_date",CONCAT('collectionMonth', LPAD(cf2.content,2,'0')) as "collection_date" FROM base_user bu,contact c,contact_field cf, invoice i, contact_field cf2 WHERE bu.id = c.user_id AND bu.deleted = 0 AND cf.contact_id = c.id AND cf.type_id = 7 AND cf.content = 'DD' AND i.user_id = bu.id AND i.balance > 0 AND i.is_review != 1 AND i.deleted != 1 AND due_date BETWEEN 'dueDateEarliest' AND 'dueDateLatest' AND cf2.contact_id = c.id AND cf2.type_id = 8 ORDER BY bu.id limit 20;
This is giving error as
ERROR: invalid input syntax for type timestamp:
I am not getting any way to fix it.
And moreover the way I am setting value using \set is it fine ?
Or should I use SET to set the values.
Because in actual when I have to run these command from a shell script I will be calling/setting as
`set dueDateEarliest '$dueDateEarliest'` from shell script.
Which is the best way ?
Attaching the screen shot as well

It's an issue with how you formatted your query. Let's simplify it a bit:
# \set dueDateEarliest '2018-04-01'
# select 'dueDateEarliest'::timestamp;
ERROR: invalid input syntax for type timestamp: "dueDateEarliest"
LINE 1: select 'dueDateEarliest'::timestamp;
It doesn't work, because it's trying to use the string 'dueDateEarliest', not the variable.
Here's the correct way:
# select :'dueDateEarliest'::timestamp;
timestamp
---------------------
2018-04-01 00:00:00
(1 row)

Related

Tableau Prep: How can I refer to a parameter in the Custom SQL query?

Using Tableau Prep Builder version 2021.4.4 I connected to Postgresql database (version 12) and created a Custom Query as the only input in the Flow.
Then I created a parameter (my_date).
In the custom query I have:
select my_field
from my_table
where date = -- How can I refer to my_date?
I have already tried the following but all failed:
where date = ${my_date}-- syntax error at or near "$"
where date = $my_date -- syntax error at or near "$"
where date = :my_date -- syntax error at or near ":"
where date = (my_date) -- "my_date" does not exist
where date = my_date -- "my_date" does not exist
where date = $1 -- Index 0 out of bounds for length 0
Use < > , for example:
where date = <my_date>
OR
where date = '<my_date>'

PostgreSQL Getting error in order to change explicit type cast

SELECT COUNT(*)
FROM WASADMIN.DAILYTXNSREPORT
WHERE UBACCOUNTID = '01ED10EOD0100'
AND UBTXNAMT = '109.63'
AND UBTYPE = 'I'
AND UBVALUEDTTM LIKE '11/7/2015 12:00:00 AM%'
AND UBTXNAMTCR = '109.63'
AND UBTXNAMTDR = '0.0'
AND UBTXNCODE = 'IAP'
AND UBTXNNARRATION = 'Fixed Narrative:Interest Application'
AND UBTXNSRCBRANCH = '70000001.0'
AND UBTXNBASEEQ = '109.63'
AND UBCHANNELID = 'UXP'
An error occurred when executing the SQL command:
SELECT COUNT(*) FROM WASADMIN.UBTB_DAILYTXNSREPORT WHERE UBACCOUNTID='01ED10EOD0100' AND UBTXNAMT='109.63' AND UBTYPE='I' AND UBVALUEDTTM LIKE '11/7/2...
ERROR: operator does not exist: timestamp without time zone ~~ unknown
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 139
Expected answer: to get the count
LIKE is for comparing string values, not for timestamps.
I am not sure what the LIKE condition is supposed to achieve, but it seem you want to find rows where `` is equal to midnight of 2015-11-07. The following would do that:
AND ubvaluedttm = timestamp '2015-11-07 00:00:00'
A range condition is probably closer to what the LIKE condition would do:
AND ubvaluedttm >= timestamp '2015-11-07 00:00:00'
AND ubvaluedttm <= timestamp '2015-11-07 00:00:00.999999'

invalid input syntax for integer: "" : Postgresql

I am doing a migration from mssql to postgresql and I am facing issue in resolving one of the queries.
My Query:
SELECT 1,
CASE
WHEN cast((case
when split_part(mt_outward_qty,'/',1) > '0' then
substring(mt_outward_qty,0,split_part(mt_outward_qty,'/',1)::int)
when split_part(mt_outward_qty,'.',1)>'0' then
substring(mt_outward_qty,0,split_part(mt_outward_qty,'.',1)::int)
else
mt_outward_qty
end) as int) > 0
THEN 'Y'
ELSE 'N'
END
from STL_CS_Tra_requestdetails
LEFT JOIN STL_CS_Tra_storeopening ON req_id = so_requestid
LEFT JOIN STL_CS_Tra_multiple_timinig ON req_id = mt_reqid
Upon executing this, I am getting:
invalid input syntax for integer: ""
Kindly help me in resolving this.
sql server automatically cast empty string to 0 when you cast to int
you can do it in postgresql like so :
( 0 || '')::int

Postgresql SQLSTATE[42P18]: Indeterminate datatype with PDO and CONCAT

I'm having issues with CONCAT() when used on a WHERE, in PDO.
The code:
<?php
require_once('config.php');
$fdate = '01/01/2010';
$tdate = '31/12/2030';
$identification = '';
$count = "SELECT count(*) as total FROM ( select time_id from doc_sent WHERE date >= :fdate AND date <= :tdate AND identification LIKE concat('%',:identification,'%') ) x;";
//$count = "SELECT count(*) as total FROM ( select time_id from doc_sent WHERE date >= :fdate AND date <= :tdate ) x;";
$stmt_count_row_main_table = $pdo->prepare($count);
$stmt_count_row_main_table->execute(['fdate' => $fdate, 'tdate' => $tdate, 'identification' => $identification]);
//$stmt_count_row_main_table->execute(['fdate' => $fdate, 'tdate' => $tdate]);
$count_row_main_table = $stmt_count_row_main_table->fetch();
print_r( $count_row_main_table);
?>
The code works when the 'identification' part is commented.
When I'm trying to use CONCAT(), it doesn't.
I tried many "version" of CONCAT() (and read many other questions, like this one: How do I create a PDO parameterized query with a LIKE statement? ) but I am always referring to the main documentation:
https://www.postgresql.org/docs/9.1/static/functions-string.html
Which say:
concat('abcde', 2, NULL, 22) --> abcde222
The FULL error when I use CONCAT() is:
PHP Fatal error: Uncaught PDOException: SQLSTATE[42P18]: Indeterminate datatype: 7 ERROR: could not determine data type of parameter $3 in /var/www/pdo-reporter/show.php:17\nStack trace:\n#0 /var/www/pdo-reporter/show.php(17): PDOStatement->execute(Array)\n#1 {main}\n thrown in /var/www/pdo-reporter/show.php on line 17
What's wrong with my code?
CONCAT is a function that takes a VARIADIC argument list, which means that internally postgres will convert them into an array of the same type.
postgres=# \df concat
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+--------+------------------+---------------------+------
pg_catalog | concat | text | VARIADIC "any" | func
When trying to resolve the input type to a single type, the SQL parser fails. It can be reproduced in this simpler form:
postgres=# PREPARE p AS select concat('A', $1);
ERROR: could not determine data type of parameter $1
The parser can't figure out the datatype of $1 so it errs on the side of caution.
One easy solution is to cast the parameter as text:
postgres=# PREPARE p AS select concat($1::text);
PREPARE
or with the CAST operator:
postgres=# PREPARE p AS select concat(cast($1 as text));
PREPARE
I haven't tested with PDO but presumably it would work (given how it deals with parameters to produce prepared statements) to change the query to:
"...identification LIKE '%' || :identification || '::text%'..."
or use the '||' operator instead of concat in the query:
identification LIKE '%' || :identification || '%'
EDIT: BTW if you want to find that a parameter :X is a substring of identification, this clause is more secure: strpos(identification, :X) > 0, because :X may contain '%' or '_' without causing any side-effect in the match, contrary to what happens with LIKE.

PostgreSQL: return message after count = 0

I have maybe easy question, but I'm completely stucked.
I have script
SELECT COALESCE(COUNT(id), 0) as MyFiels from table
It works fine and when I have zero value it shows 0.
But I want that instead of 0, I can see one line = "NO RESULTS" for example.
I tried:
SELECT COALESCE(to_char(COUNT(id), 'NO RESULT')) as MyFiels from table
And PostgreSQL shows error message:
ERROR: "E" is not supported
SQL state: 0A000
Where I'm incorrect? Any ideas?
I see what is the error, you are trying to use coalesce to convert 0 to string, and coalesce convert null to something. You need use a CASE
SELECT CASE WHEN COUNT(*) = 0 THEN 'NO RESULT'
ELSE CAST(COUNT(*) as TEXT)
END as field
FROM Table