When I run the following statement in postgres it removes the letter 't' from the resulting tsquery:
Statement: select websearch_to_tsquery('english', 'xtp t');
Result: xtp
Same result with underscore separating:
Statement: select websearch_to_tsquery('english', 'xtp_t');
Result: xtp
However, if I substitute the 't' for any other letter I get the expected result:
Statement: select websearch_to_tsquery('english', 'xtp_l');
Result: 'xtp' <-> 'l'
Why does the letter 't' get removed from the result of the original query?
Not any other letter. i, a, s, and t are considered stop words in English. 'I' and 'a' actually are very common words, s and t are presumably included because they occur in contractions and possessives, like "I can't stand dog's slobbery toys"
Related
In PostgreSQL you can do a case insensitive query with ILIKE:
select * from test where value ilike 'half is 50$%' escape '$'
And you can query multiple values at once by combining ILIKE with ANY()
select * from test where value ilike any(array['half is 50%', 'fifth is 20%'])
The query above will match 'Fifth is 2019', which I do not want, but when I try to use ILIKE and ANY() with an escape character I get a syntax error.
Am I missing something stupid, or is this simply not supported? If not, is there another way to query in a case insensitive way with multiple values at once?
EDIT: To clarify, the query will accept parameters through JDBC, so the actual SQL will look something like
select * from test where value ilike any(?) escape '$'
This is why I'm looking make % and _ from the user input be interpreted as literals.
The ESCAPE clause in ILIKE refers only to literals and does not apply to expressions. You should use a backslash, or if not possible, you can try:
with test(value) as (
values
('half is 50%'),
('half is 50x'),
('fifth is 20%'),
('fifth is 2000')
)
select *
from test
where value ilike any(select replace(unnest(array['half is 50$%', 'fifth is 20$%']), '$', '\'))
value
--------------
half is 50%
fifth is 20%
(2 rows)
Looks a bit clumsy but works well.
To match them as raw strings, you may use the ~* operator for insensitive match.
knayak=# select 'Half is 50%' ~* any(array['half is 50%', 'fifth is 20%'])
knayak-# ;
?column?
----------
t --True
(1 row)
knayak=# select 'fifth is 20' ~* any(array['half is 50%', 'fifth is 20%']);
?column?
----------
f --False
(1 row)
If you wish to escape the right hand operands of ilike use "escape" string constants, which are an extension to the SQL standard. An escape string constant is specified by writing the letter E (upper or lower case) just before the opening single quote
knayak=# select 'Half is 50%' ilike any(array[E'half is 50\\%', E'half is 20\\%'])
knayak-# ;
?column?
----------
t
(1 row)
DEMO
say I have a table 't1' with a string column 'name'. And I have names 'n1','n2','n9' and 'n:'. If I do
select * from t1 orderby name asc
I expect
n1
n2
n9
n:
Given that ':' comes after '9' in ASCII, but instead, I get
n:
n1
n2
n9
Which is a surprise. Is there something I need to do to say 'use ASCII as the collating sequence for basic ASCII chars'
From my experience, this is a collation issue
SELECT * FROM t1 ORDER BY name COLLATE "POSIX";
This is a list of exapmle collations in case that collation have listed, SQL_Latin1_General_CP850_BIN does not work
https://www.postgresql.org/docs/9.1/static/collation.html
Example
I have a string...
'/this/is/a/given/string/test.file'.
How can I get substring 'given/string/test.file' in PSQL?
Thank you!
You can use a regular expression
with example(str) as (
values('/this/is/a/given/string/test.file')
)
select regexp_replace(str, '(/.*?){4}', '')
from example;
regexp_replace
------------------------
given/string/test.file
(1 row)
or the function string_to_array():
select string_agg(word, '/' order by ord)
from example,
unnest(string_to_array(str, '/')) with ordinality as u(word, ord)
where ord > 4;
Read also How to find the 3rd occurrence of a pattern on a line.
I dont know how to get the nth occurence of a substring, but for this problem, you can use regular expression. Like this:
select substring('/this/is/a/given/string/test.file' from '/[^/]+/[^/]+/[^/]+/(.*)')
You can improve the regular expression, this is just for demo purpose.
Sorting column #7 as an example -
This code does not sort data at all:
ORDER BY CASE WHEN '1'='2' THEN 5
WHEN '1'='1' THEN 7
ELSE 13 END
If I change it to a hard-coded ordinal it works:
ORDER BY 7
As long as the respective expressions in the SELECT list are of the same type, you can do it by using the expressions themselves instead of the SELECT list number:
SELECT expression1, expression2, ...
...
ORDER BY CASE
WHEN 1=2
THEN expression5
WHEN 1=1
THEN expression7
ELSE expression13
END;
If the data types are not the same, season with type casts.
Your query does not work because only integer literals can be used as column numbers in ORDER BY. In all other cases, an integer just stands for its constant value.
If it were not like this, ORDER BY expressions could easily become ambiguous. Look at the following:
... ORDER BY intcol + 3;
Should that mean “add three” or “add expression number three from the SELECT list”?
I'm working with PostgreSQL and want to know whether you can have a wildcard retain its value.
So for example say I had
select * from tableOne where field like ‘_DEF_’;
Is there a way to get the first and last wildcard to be the exact same character?
So an example matching result could be: ADEFA or ZDEFZ.
You can use a regular expression with a back-reference:
select *
from some_table
where some_column ~* '^(.)DEF(\1)$'
^(.)DEF(\1)$ means: some character at the beginning followed DEF followed by the first character must occur at the end of the string.
The () defines a group and the \1 references the first group (which is the first character in the input sequence in this example)
SQLFiddle example: http://sqlfiddle.com/#!15/d4c4d/1
Use regular expression:
with test as (
select 'xABa' as foo
union select 'xABx'
union select 'xJBx'
)
select * from test
where foo ~* E'^(.)AB\\1$'
Outputs:
foo
------
xABx
(1 row)