I have a "product" table with a varchar[] column to keep excluded companies.
When I select the array for a specific product, I get it like this:
SELECT excluded_company_codes FROM product WHERE code = '123'
excluded_company_codes
----------
{'10'}
However, oddly enough, when I try to check if company code exists in the array with ANY function, it doesn't work:
SELECT '10'=ANY(product.excluded_company_codes) where code = '123'
?column?
----------
false
What am I doing wrong here?
The string in the array contains two single quotes. If you want to find that, you have to
SELECT '''10''' = ANY(product.excluded_company_codes)
FROM product
WHERE code = '123';
If you want to avoid doubling the quotes, you can use dollar quoting: $$'10'$$.
Related
I have a requets which giving me an ids. I need to iterate them into another request, so I have a sheme like this: scheme
In tPostgresqlInput I have this code rc.id = upper('18ce317b-bf69-4150-b880-2ab739eab0fe') , but instead of id I need to put smthn like globalMap.get(row4.id). How did I do this?
Apparently this is a syntax issue
Try with :
"select * FROM table LEFT JOIN table on parameter JOIN table on parameter
WHERE 1=1 AND
column = 'content'
AND upper(rc.id) = upper('"+((String)globalMap.get("row4.id")) +"')"
Expressions in tDBInput should always begin and end with double quotes.
Don't forget to cast globalMap.get() with the type of your element (here I put String)
.equals is not a DB function but a java function. I have replaced it with '='
Let me know if it's better
I have a VIEW like this:
SELECT * FROM test --will show:
path
------------------------
/downloads/abc-dbc-abcd
/downloads/dfg-gfd-hjkl
/downloads/tyu-iti-titk
How do I use TRIM to only select the trailing part of the strings in column path?
In PostgreSQL, I've tried:
SELECT TRIM('/downloads/' FROM (SELECT * FROM test);
SELECT TRIM('/downloads/' FROM (SELECT path FROM test);
I expect to receive the output strings as just 'abc-dbc-abcd', etc.; the same as input but with '/downloads/' removed. I have been getting an error...
ERROR: more than one row returned by a subquery used as an expression
Your error are because you are using SubQuery in your TRIM() function and it returned more than 1 row so the error show.
And I prefer you use REPLACE() rather than TRIM() function here. From Documentation REPLACE :
Replace all occurrences in string of substring from with substring to
For the query :
SELECT REPLACE(path, '/downloads/', '') from test;
You can see here for Demo
Try this.
SELECT LTRIM(RTRIM(REPLACE(path,'/downloads/','')))
right now I have a keyword array like:
['key1', 'key2', 'key3'.......] , the keyword can be number or character.
If I want to search in my table (postgres database), and find out all record contain any of keyword in that array, how can I do that?
For example:
I got a table which has a column called name and a column called description
I need find all record that either name or description contains any keywords in that array.
thanks
Maybe this example will be useful:
CREATE TABLE TEST(
FIELD_KEY TEXT);
INSERT INTO TEST VALUES('this is hello');
INSERT INTO TEST VALUES('hello');
INSERT INTO TEST VALUES('this');
INSERT INTO TEST VALUES('other message');
SELECT *
FROM TEST
WHERE FIELD_KEY LIKE ANY (array['%this%', '%hel%']);
This will return:
this is hello
hello
this
Here other example:
SELECT *
FROM TEST
WHERE FIELD_KEY ~* 'this|HEL';
~* is case insensitive, ~ is case sensitive
You can try this example here.
select *
from t
where
array[name] <# my_array
or array[description] <# my_array
Couple the like operator with the any subquery expression:
select *
from t
where name like any (values ('%John%'), ('%Mary%'))
Or the array syntax:
where name like any (array['%John%', '%Mary%'])
I want a query which will return a combination of characters and number
Example:
Table name - emp
Columns required - fname,lname,code
If fname=abc and lname=pqr and the row is very first of the table then result should be code = ap001.
For next row it should be like this:
Fname = efg, lname = rst
Code = er002 and likewise.
I know that we can use substr to retrieve first letter of a colume but I don't know how to use it to do with two columns and how to concatenate.
OK. You know you can use substr function. Now, to concatenate you will need a concatenation operator ||. To get the number of row retrieved by your query, you need the rownum pseudocolumn. Perhaps you will also need to use to_char function to format the number. About all those functions and operators you can read in SQL reference. Anyway I think you need something like this (I didn't check it):
select substr(fname, 1, 1) || substr(lname, 1, 1) || to_char(rownum, 'fm009') code
from emp
I've got a table that resembles the following:
WORD WEIGHT WORDTYPE
a 0.3 common
the 0.3 common
gray 1.2 colors
steeple 2 object
I need to pull the weights for several different words out of the database at once. I could do:
SELECT * FROM word_weight WHERE WORD = 'a' OR WORD = 'steeple' OR WORD='the';
but it feels ugly and the code to generate the query is obnoxious. I'm hoping that there's a way I can do something like (pseudocode):
SELECT * FROM word_weight WHERE WORD = 'a','the';
You are describing the functionality of the in clause.
select * from word_weight where word in ('a', 'steeple', 'the');
If you want to pass the whole list in a single parameter, use array datatype:
SELECT *
FROM word_weight
WHERE word = ANY('{a,steeple,the}'); -- or ANY('{a,steeple,the}'::TEXT[]) to make explicit array conversion
If you are not sure about the value and even not sure whether the field will be an empty string or even null then,
.where("column_1 ILIKE ANY(ARRAY['','%abc%','%xyz%']) OR column_1 IS NULL")
Above query will cover all possibility.