I need to lowercase array content in PostgreSQL, lower(array['array_content'])) or array[lower('array_content'])) does not work. Actual array is much, much longer.
SELECT * FROM kliendi_aadress WHERE lower(linn) LIKE ANY (array['Tallinn', 'Tartu','Narva'])
Can this even be done?
Well ILIKE solved this problem for me
SELECT * FROM kliendi_aadress WHERE linn ILIKE ANY (array['Tallinn', 'Tartu','Narva']);
the first that come to mind is ugly:
db=# select lower(array['Tallinn', 'Tartu','Narva']::text)::text[];
lower
-----------------------
{tallinn,tartu,narva}
(1 row)
Here I lower text representation of your array and then cast it back to array.
And so comparison:
db=# select 'tartu' = any (lower(array['Tallinn', 'Tartu','Narva']::text)::text[]);
?column?
----------
t
(1 row)
The citext extension can come in handy in this situation.
-- If you haven't enabled citext in your DB
CREATE EXTENSION citext;
select 'tartu' = any (array['Tallinn', 'Tartu','Narva']::citext[])
should work.
Related
The query:
select ;
is valid in postgresql. It returns a tuple with no attribute.
# select ;
--
(1 row)
It has clear semantics and the result can be used as a subquery:
# select 1 from (select ) as rip;
?column?
----------
1
(1 row)
In fact, one can create a table with no attributes using it. One can even add tuples to it!!
But my question is, why does it exist?
I see value in a select without a from clause, as psql can be used as a calculator:
# select 3 * 6;
?column?
----------
18
(1 row)
and or be used to call a UDF.
But I can not envision a use for select ;
it is useful or is it an oddity of postgresql's parser?
See here:
https://www.postgresql.org/docs/current/sql-select.html
Empty SELECT Lists
The list of output expressions after SELECT can be empty, producing a zero-column result table. This is not valid syntax according to the SQL standard. PostgreSQL allows it to be consistent with allowing zero-column tables. However, an empty list is not allowed when DISTINCT is used.
In postgresql, I have mangaged to add wildcard pattern (*) to the query using SIMILAR TO option. So my query will be,
SELECT * FROM table WHERE columnName SIMILAR TO 'R*'
This query would return all entities starting from 'R' and not 'r'. I want to make it case insensitive.
Use ILIKE:
SELECT * FROM table WHERE columnName ILIKE 'R%';
or a case-insensitive regular expression:
SELECT * FROM table WHERE columnName ~* '^R.*';
Both are PostgreSQL extensions. Sanjaya has already outlined the standards-compliant approaches - filtering both sides with lower(...) or using a two-branch SIMILAR TO expression.
SIMILAR TO is less than lovely and best avoided. See this earlier answer.
You could write:
SELECT * FROM table WHERE columnName SIMILAR TO '(R|r)%'
but I don't particularly recommend using SIMILAR TO.
try
SELECT * FROM table WHERE columnName SIMILAR TO 'R%|r%'
Is there a better way to get a row of a table into hstore format than going
SELECT hstore(ARRAY['col1','col2','col3'], ARRAY[col1::text, col2::text, col3::text]) FROM tbl;
It works, but I figure there has to be a better way than typing out each column. hstore takes a record type for input, but I couldn't figure out how to feed the single-row producing query into the function and make it happy. Postgres version 9.0.4.
Yes - you can cast row to hstore type with hstore() function.
SELECT hstore(tbl.*) FROM tbl;
Works for me:
filip#filip=# select hstore(foo.*) from foo;
hstore
------------------------
"bar"=>"1", "baz"=>"2"
(1 row)
See http://www.postgresql.org/docs/9.0/static/hstore.html#HSTORE-FUNC-TABLE
I cannot get the like statement to work with space and trailing wildcard.
My query goes as follows:
select * from Table where Field like 'Desc_%'
The data is space-delimited such as, Desc Top, Desc Bottom and so on. The query works when I use the pattern 'Desc_%' but not when I use the pattern 'Desc %'. The field is nvarchar(255).
Any ideas?
EDIT
Turns out the data was tab-delimited and when I copied a value from the 2008 Management Studio it converted the tab to space. Dumb mistake. I did like the [ ] tip so I marked it the answer. Thanks everyone, I'll remember not to trust the copy from the grid results.
Use brackets '[' & ']' to set up a single-character class to match. In your case the SQL should look like this: "select * from Table where Field like 'Desc[ ]%'"
EDIT: add sample, link
CREATE TABLE #findtest (mytext varchar(200) )
insert #findtest VALUES ('Desc r')
insert #findtest VALUES ('Descr')
select * from #findtest where mytext like 'Desc[ ]%'
DROP TABLE #findtest
(1 row(s) affected)
(1 row(s) affected)
mytext
--------
Desc r
(1 row(s) affected)
See this article.
Since an underscore is a single character wildcard, and percent is the multi-char wildcard, they are the same ( "%" and "_%" ). It is as if you are asking for two consecutive wildcards. Not sure if I understand your question, but I am not surprised it does not behave the way you expect.
Consider explicitly stating that you want a space, using its ASCII value?
SELECT * FROM Table WHERE Field Like 'Desc' + CHAR(32) + '%'
In Postgresql 8 why this is ok
select * from prod where code like '1%'
select * from prod where code like '%1'
but this returns 0 rows (there are codes begining/ending with digit 1)
select * from prod where code like '1%1'
Update
That happens in my current instalation:
# psql --version
psql (PostgreSQL) 8.3.7
create table a(code char(10));
CREATE TABLE
db=# insert into a values('111');
INSERT 0 1
db=# select * from a where code like '1%';
code
------------
111
(1 row)
db=# select * from a where code like '%1';
code
------
(0 rows)
db=# select * from a where code like '1%1';
code
------
(0 rows)
Update 2
It is the datatype ! With varchar it is Ok !
Thank you.
Is it because the datatype is char(10)?
This means that it will always occupy 10 characters even though you just insert something shorter like "111". Therefore, if you don't use a 10-characters string with "1" at the end, "%1" and "1%1" will never match.
(EDIT: I had posted the following (with with an AND operator, rather than OR).
SELECT * FROM prod WHERE code LIKE '%1' OR code LIKE '1%';
If you want AND operator, the query in the question should work OK. However, if you want to use OR operator, then my above query is probably one of the better ways of doing it.