Hit a bit of a brickwall here, I haven't found anything that seems to work.
SELECT *
FROM glsltransaction gls
INNER JOIN glhistory h ON gls.sltrxid = h.schedxrefid
INNER JOIN apvendor ap ON ap.vendorid = gls.acctid
INNER JOIN glchartofaccounts coa USING (acctdeptid)
WHERE h.description <> gls.description
AND h.description not like || ap.name || '%'
AND gls.description <> ''
This line here AND h.description not like || ap.name || '%' is what i'm having issues with.
I get the following error when trying to run that statement above:
'Error occurred in running query from editor : ERROR: operator does not exist: || text Hint: No operator matches the given name and argument type. You might need to add an explicit type cast. Position: 563'
I'm effectively wanting it to function like a.column not like b.column%
Any help will be greatly appreciated, thanks!
Related
What is the proper way to write this query in postgreSQL?
I am trying to normalize (i.e. standardize) an address. I can use pagc with no problem if I feed it a hardcoded address. However, I need to feed it an address parsed from parts. I see there are several similar questions here on stack overflow referencing the same error. These queries are complex and are all pretty different from mine, so I couldn't get to the solution from reading the other posts.
I have tried:
with full_address as (home_address1 || ','|| home_city ||','|| home_state ||,','|| home_zip) update contacts set (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num) = (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal) FROM pagc_normalize_address(full_address) AS addy where contact_id = 833826;
This throws error:
syntax error at or near "home_address1"
LINE 26: with full_address as (home_address1 || ','|| home_city |.
I have also tried:
update contacts set (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num) = (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal) FROM pagc_normalize_address(home_address1 ||','||home_city||','||home_state||','||','||home_zip) AS addy where contact_id = 833826;
Error:
ERROR: invalid reference to FROM-clause entry for table "contacts"
LINE 24: ...abbrev,addy.internal) FROM pagc_normalize_address(home_addre...
^
HINT: There is an entry for table "contacts", but it cannot be referenced from this part of the query.
SQL state: 42P10
Character: 2297
The first query is gibberish, the second makes sense but fails because you cannot use a lateral reference to the updated table's columns in the FROM clause.
Try a CTE like this:
WITH addy AS (
SELECT addy.* FROM
contacts
CROSS JOIN LATERAL
pagc_normalize_address(home_address1
|| ',' || home_city || ',' || home_state || ',' || ',' || home_zip) AS addy
WHERE contacts.contact_id = 833826
)
UPDATE contacts
SET (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num)
= (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal)
FROM addy
WHERE contact_id = 833826;
I'm trying to check if some text contains the concatenation of a text and a value from an array in Postgres, something like:
SELECT true from jsonb_array_elements('["a", "b"]'::jsonb) as ids
WHERE 'bar/foo/item/b' LIKE '%item/' || ids->>'id' || '%'
I'm getting the following error:
ERROR: invalid input syntax for type json Detail: Token "%" is invalid. Position: 95 Where: JSON data, line 1: %...
How can I make use of the values of the array, concatenate them with the text and check the LIKE expression?
I have tried several ideas of explicitly adding a cast like ::jsonb, but no luck so far.
The problem is that the || and ->> operators have the same precedence and are left associative, so the expression is interpreted as
(('%item/' || ids) ->>'id') || '%'
You'd have to add parentheses:
'%item/' || (ids->>'id') || '%'
Finally got this working, this is the result:
SELECT true from jsonb_array_elements_text('["a", "c"]'::jsonb) as ids
WHERE 'bar/foo/item/b' LIKE '%item/' || ids.value || '%'
The key changes were to use jsonb_array_elements_text instead of jsonb_array_elements and ids.value instead of ids->>'id'
I have a table with an integer column. It has 12 records numbered 1000 to 1012. Remember, these are ints.
This query returns, as expected, 12 results:
select count(*) from proposals where qd_number::text like '%10%'
as does this:
SELECT COUNT(*) FROM "proposals" WHERE (lower(first_name) LIKE '%10%' OR qd_number::text LIKE '%10%' )
but this query returns 2 records:
SELECT COUNT(*) FROM "proposals" WHERE (lower(first_name) || ' ' || qd_number::text LIKE '%10%' )
which implies using || in concatenated where expressions is not equivalent to using OR. Is that correct or am I missing something else here?
You probably have nulls in first_name. For these records (lower(first_name) || ' ' || qd_number::text results in null, so you don't find the numbers any longer.
using || in concatenated where expressions is not equivalent to using ORIs that correct or am I missing something else here?
That is correct.
|| is the string concatenation operator in SQL, not the OR operator.
My website is just working fine til i deployed it to heroku and the problem is heroku uses pgsql and I'm using mysql and laravel framework.
my query is
$patient = Patient::where('patient_address', 'ILIKE' ,'%' . $request->input)->where('patient_sex', 'ILIKE' ,'%' . $request->gender)->whereHas('users', function($q) use($vaccine_id){
$q->where('vaccine_id','ILIKE','%' . $vaccine_id);
})->get();
here's what I'm getting when I deploy it to heroku
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: integer ~~* unknown
LINE 1: ...ient_id" = "patients"."PatientID" and "vaccine_id" ILIKE $3)
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. (SQL: select * from "patients" where "patient_address" ILIKE %San Francisco and "patient_sex" ILIKE % and exists (select * from "vaccines" inner join "immunizations" on "vaccines"."VaccineID" = "immunizations"."vaccine_id" where "immunizations"."patient_id" = "patients"."PatientID" and "vaccine_id" ILIKE %))
I have tried using cast like CAST(vaccine_id AS VARCHAR) and I' not getting the error but it doesnt return any result.
The problem is here:
$q->where('vaccine_id','ILIKE','%' . $vaccine_id)
looks like vaccine_id is integer, and you can not use operator ILIKE to integer. Try just '='
If you want to use LIKE, ILIKE or other text operator you must cast your data to text. In SQL it must looks like:
WHERE "vaccine_id"::text ILIKE val
instead
WHERE "vaccine_id" ILIKE val
You could do this:
$q->where('cast(vaccine_id AS VARCHAR)','LIKE','%' . $vaccine_id)
OR
$q->where('cast(vaccine_id AS TEXT)','LIKE','%' . $vaccine_id)
I apologize I'm very new to PostgreSQL. When I try and run a migration that works for everyone else, I get errors on my computer. I took the script that's failing from the migration and ran it in pgAdmin3 to see what was going on.
Here is my query:
DROP VIEW IF EXISTS vw_admin_question_export_text;
CREATE VIEW vw_admin_question_export_text AS
select name || '|' || q.question_key || '|' || qt.type_name || '|' || qo.options import from question q
full JOIN (SELECT question_id, COALESCE(string_agg(option_text || '|' || option_value, '|'),'') as options
FROM question_option
GROUP BY question_id) qo
on (q.question_id = qo.question_id)
JOIN question_type qt
on (q.questiontype_id = qt.questiontype_id);
This is the error I'm getting:
ERROR: syntax error at or near "import"
LINE 4: ...n_key || '|' || qt.type_name || '|' || qo.options import fro...
^
********** Error **********
ERROR: syntax error at or near "import"
SQL state: 42601
Character: 179
My coworker thinks it may be an issue with my PostgreSQL version which is 9.5.2
The problem here is that import is a reserved keyword in PostgreSQL, so you can't use it as an alias without double-quoting it or using AS keyword.
Simpler test:
postgres=# SELECT 'foo' import;
ERROR: 42601: syntax error at or near "import"
LINE 1: SELECT 'foo' import;
^
LOCATION: scanner_yyerror, scan.l:1082
postgres=# SELECT 'foo' AS import;
import
--------
foo
(1 row)
postgres=# SELECT 'foo' "import";
import
--------
foo
(1 row)
To solve your issue, you can simple add AS keyword to define the alias:
CREATE VIEW vw_admin_question_export_text AS
select
name || '|' || q.question_key || '|' || qt.type_name || '|' || qo.options AS import
from question q
full JOIN (
SELECT
question_id,
COALESCE(string_agg(option_text || '|' || option_value, '|'),'') as options
FROM question_option
GROUP BY question_id
) qo
on (q.question_id = qo.question_id)
JOIN question_type qt
on (q.questiontype_id = qt.questiontype_id);
Another option is to simple chose another name, that does not collide with one in reserved keyword list (I recommend that option). In any case, always use AS is a good idea to make things more explicit and avoid problems like this one.