PostgreSQL error witn Laravel - postgresql

I'm trying to make a request and I have an error when I'm using LIKE on the created_at field.
I'm using Laravel 5.3 with Eloquent (ORM).
$date = Carbon::now();
foreach ($hours as $hour) {
$chart[$hour]['hour'] = $hour;
$chart[$hour]['allowed'] = VisitsAllowed::where('created_at', 'LIKE', Carbon::parse($date)->format('Y-m-d %'))->count();
$chart[$hour]['denied'] = VisitsDenied::where('created_at', 'LIKE', Carbon::parse($date)->format('Y-m-d %'))->count();
}
Error :
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: timestamp without time zone ~~* unknown
LINE 1: ... aggregate from "visits_allowed" where "created_at" LIKE $1
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. (SQL: select count(*) as aggregate from "visits_allowed" where "created_at" LIKE 2017-05-30 %)
Can someone help me to find a solution.

Please try this instead :
foreach ($hours as $hour) {
$chart[$hour]['hour'] = $hour;
$chart[$hour]['allowed'] = VisitsAllowed::where('created_at', '=', Carbon::parse($date)->format('Y-m-d'))->count();
$chart[$hour]['denied'] = VisitsDenied::where('created_at', '=', Carbon::parse($date)->format('Y-m-d'))->count();
}

Not the prettiest solution but testing if the date (say 2017-05-30) is between: yyyy-mm-dd 00:00:00 and yyyy-mm-dd 23:59:59 works.
$chart[$hour]['allowed'] = VisitsAllowed::where('created_at', '>=', Carbon::parse($date )->format('Y-m-d') . ' 00:00:00')
->where('created_at', '<=', Carbon::parse($date )->format('Y-m-d') . ' 23:59:59')
->count();
$chart[$hour]['denied'] = VisitsDenied::where('created_at', '>=', Carbon::parse($date )->format('Y-m-d') . ' 00:00:00')
->where('created_at', '<=', Carbon::parse($date )->format('Y-m-d') . ' 23:59:59')
->count();

Related

CASE expression in WHERE clause Postgresql

I have been struggling to figure out how to put a CASE expression in the WHERE clause using PostgreSQL. I need to convert the string to a date (as seen in line 3). This works fine. When I try to pull CURRENT_DATE in the WHERE clause I run into errors. Is this the best way to do this? Any suggestions would be very much welcomed.
SELECT
CASE WHEN
multi_app_documentation.nsma1_code = 'DATE' THEN TO_DATE(multi_app_documentation.nsma1_ans, 'MMDDYYYY') END AS "Procedure Date",
' ' AS "Case Confirmation Number",
ip_visit_1.ipv1_firstname AS "Patient First",
ip_visit_1.ipv1_lastname AS "Patient Last",
visit.visit_sex AS "Patient Gender",
TO_CHAR(visit.visit_date_of_birth, 'MM/DD/YYYY') AS "DOB",
visit.visit_id AS "Account Number",
visit.visit_mr_num AS "MRN",
' ' AS "Module",
' ' AS "Signed off DT",
CASE WHEN
multi_app_documentation.nsma1_code = 'CRNA' THEN multi_app_documentation.nsma1_ans END AS "Primary CRNA",
' ' AS "Secondary CRNA",
' ' AS "Primary Anesthesiologist",
' ' AS "Secondary Anesthesiologist",
' ' AS "Canceled Yes/No"
FROM
multi_app_documentation
INNER JOIN ip_visit_1 ON multi_app_documentation.nsma1_patnum = ip_visit_1.ipv1_num
INNER JOIN visit ON ip_visit_1.ipv1_num = visit.visit_id
WHERE
CASE
WHEN ( multi_app_documentation.nsma1_code = 'DATE' AND TO_DATE( multi_app_documentation.nsma1_ans, 'MMDDYYYY' ) = CURRENT_DATE END )
AND multi_app_documentation.nsma1_ans IS NOT NULL
ORDER BY
ip_visit_1.ipv1_lastname ASC
Your parentheses are unbalanced. Also, you don't even need a CASE expression in the WHERE clause. Use this version:
WHERE
multi_app_documentation.nsma1_code = 'DATE' AND
TO_DATE(multi_app_documentation.nsma1_ans, 'MMDDYYYY') = CURRENT_DATE
In addition, it is undesirable to store text dates in the nsma1_ans column. It would be better to use a bona-fide date column. If you must store the dates as text, then at least use the format YYYY-MM-DD, which then could simply be cast to date using nsma1_ans::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'

why do i have a date error in my sql query?

I am doing an SQL SELECT query but I have the error message:
"SQL Error [22007]: [SQL0181] A value of date, time, or timestamp
string is incorrect."
Here is my request:
SELECT *
FROM ROXDTA400.STKF0300 A
JOIN ROXDTA400.TABJ00141 B ON A.STNSIT = B.CDSITE
WHERE ( A.STNLIB <> '-- Trémie --'
AND A.STNSIT <> 40
AND DATE(LEFT(STNDAV,4) || '-' || substr(STNDAV,5,2) || '-' || RIGHT(STNDAV,2))
BETWEEN DATE('2019-01-01') AND DATE('2019-01-04') );
The problem seems to come from the date created with the STNDAV field, because if I replace with for example DATE ('2019-01-03'), it works.
DATE (LEFT (STNDAV, 4) || '-' || substr (STNDAV, 5,2) || '-' || RIGHT (STNDAV, 2)) Gives me the correct date format.
Where would the problem come from?
thank you,
Ensure that dates stored in STNDAV are valid. I mean, check there is any invalid date such as February 30th or '99999999'. If the source is an IBM i (iSeries or AS/400), it will be faster if you avoid functions in the WHERE portion, so STNDAV BETWEEN '20190101' AND '20190104' will perform better.

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.

Find statement with alias

I'm trying to group a query's response by day, month, or year based on user input.
To do this I'm extracting the day, month, or year from the log_time column AS when and then trying to GROUP BY when but CakePHP says:
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "when" LINE 1: ...2-31' GROUP BY "Passenger"."route_id", "Route"."name", when ^
Here's an example of my function call where the grouping is 'month'.
$fields = array(
"CONCAT(DATE_PART('year', \"Passenger\".\"log_time\"), '-', DATE_PART('month', \"Passenger\".\"log_time\")) AS when",
"Passenger.route_id",
"Route.name",
"SUM(Passenger.embarked) AS embarked"
);
$conditions = array(
"Passenger.log_time >=" => $start,
"Passenger.log_time <=" => $end
);
$group = array("Passenger.route_id", "Route.name", "\"when\"");
return $this->find('all', array('fields' => $fields, 'conditions' => $conditions, 'group' => $group));
This is using Postgres on Cake 2.3.5. Has anyone else figured out how to do this?
when is a keyword that is used with CASE. Looks like PostgreSQL can figure out that the when in your select is just an identifier but there isn't enough context to figure it out in the GROUP BY. I'd quote it in both places:
"CONCAT(DATE_PART('year', \"Passenger\".\"log_time\"), '-', DATE_PART('month', \"Passenger\".\"log_time\")) AS \"when\"",
and:
$group = array("\"when\"", "Passenger.route_id", "Route.name");