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.
Related
I have a query -
select whereto, id from table where id=1;
I need to find the position of where in this query. It should not consider the column whereto.
How we can achieve it in postgres?
I could try this on psql on Mac Big Sur.
postgres=# \set myvar 'select whereto, id from table where id=1;';
And if I try where (with a space), it gives me -
postgres=# select strpos(:'myvar','where ');
strpos
--------
31
(1 row)
As per the documentation, strpos takes only string and not regex. - https://www.postgresql.org/docs/current/functions-string.html
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.
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.
In psql, with \x toggled to expanded output mode, I get these very long wrapped dashed lines for record separators when there is a field with a long string value in one of the selected records. They look like
-[ RECORD 2 ]----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (is much longer)
Is there any way to suppress or shorten these lines? I'm on PostgreSQL 8.4
Try \t:
test=# select * from test limit 1;
-[ RECORD 1 ]-------------------
id | 1
name | foo
test=# \t
Showing only tuples.
test=# select * from test limit 1;
id | 1
name | foo
Docs.
Also try:
test=# \pset border 0
Border style is 0.
backend=# select * from test limit 2;
id 1
name foo
id 2
name bar
I had the same issue, using these two psql command line flags solved the issue for me:
\x (good for records with lots of columns)
\pset format wrapped (wraps the postgres output to your terminal width)
I got the response from this dba stackexchange article
I had this issue and just learned about https://www.pgcli.com/, and it fixes this issue by default, and has a host of other features.
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) + '%'