Full-text search with wildcard - search

I have a table with full text search enabled. But I can't get query the table using wildcard.
select * from products where contains(Description, 'Computer') returns rows with the word "Computer"
select * from products where contains(Description, 'Compute*') [replace "r" with "*"] returns nothing
What's going on?

Assuming SQL Server, add double quotes around the wildcarded expression like so
SELECT *
FROM products
WHERE contains(Description, '"Compute*"')

Related

PostgreSQL query where column is represented as a string

I'm using Retool, and trying to run a query where the column value comes from a drop-down list. The value output is a string, so my query looks like this:
select * from accounts where {{dropDownList.value}} ilike {{'%' + account_search_textInput.value + '%'}}
When the query runs, it is as follows:
select * from accounts where "first_name" ilike '%Adam%';
The double quotes around the column name first_name seem to be causing an issue but I don't think I can remove them. Is there any other way to successfully run the query where first_name can represent the column name rather than a string value?

I used 'LIKE' on searching after 2 dept for the same column But no result...!

SELECT *
FROM JOBS_table
my like such following;
WHERE Job_id (1st) LIKE 'IT%'
AND Job_id (2nd) LIKE 'SA%';
You are requesting a field, which is starting with IT AND starting with SA, which will never be satisfyable.
Either you use "OR" or if you search in a string with "LIKE %IT%" AND "%SA%".

WHERE variable = ( subquery ) in OpenSQL

I'm trying to retrieve rows from a table where a subquery matches an variable. However, it seems as if the WHERE clause only lets me compare fields of the selected tables against a constant, variable or subquery.
I would expect to write something like this:
DATA(lv_expected_lines) = 5.
SELECT partner contract_account
INTO TABLE lt_bp_ca
FROM table1 AS tab1
WHERE lv_expected_lines = (
SELECT COUNT(*)
FROM table2
WHERE partner = tab1~partner
AND contract_account = tab1~contract_account ).
But obviously this select treats my local variable as a field name and it gives me the error "Unknown column name "lv_expected_lines" until runtime, you cannot specify a field list."
But in standard SQL this is perfectly possible:
SELECT PARTNER, CONTRACT_ACCOUNT
FROM TABLE1 AS TAB1
WHERE 5 = (
SELECT COUNT(*)
FROM TABLE2
WHERE PARTNER = TAB1.PARTNER
AND CONTRACT_ACCOUNT = TAB1.CONTRACT_ACCOUNT );
So how can I replicate this logic in RSQL / Open SQL?
If there's no way I'll probably just write native SQL and be done with it.
The program below might lead you to an Open SQL solution. It uses the SAP demo tables to determines the plane types that are used on a specific number of flights.
REPORT zgertest_sub_query.
DATA: lt_planetypes TYPE STANDARD TABLE OF s_planetpp.
PARAMETERS: p_numf TYPE i DEFAULT 62.
START-OF-SELECTION.
SELECT planetype
INTO TABLE lt_planetypes
FROM sflight
GROUP BY planetype
HAVING COUNT( * ) EQ p_numf.
LOOP AT lt_planetypes INTO DATA(planetype).
WRITE: / planetype.
ENDLOOP.
It only works if you don't need to read fields from TAB1. If you do you will have to gather these with other selects while looping at your results.
For those dudes who found this question in 2020 I report that this construction is supported since ABAP 7.50. No workarounds are needed:
SELECT kunnr, vkorg
FROM vbak AS v
WHERE 5 = ( SELECT COUNT(*)
FROM vbap
WHERE kunnr = v~kunnr
AND vkorg = v~vkorg )
INTO TABLE #DATA(customers).
This select all customers who made 5 sales orders within some sales organization.
In ABAP there is no way to do the query as in NATIVE SQL.
I would advice not to use NATIVE SQL, instead give a try to SELECT/ENDSELECT statement.
DATA: ls_table1 type table1,
lt_table1 type table of table1,
lv_count type i.
SELECT PARTNER, CONTRACT_ACCOUNT
INTO ls_table1
FROM TABLE1.
SELECT COUNT(*)
INTO lv_count
FROM TABLE2
WHERE PARTNER = TAB1.PARTNER
AND CONTRACT_ACCOUNT = TAB1.CONTRACT_ACCOUNT.
CHECK lv_count EQ 5.
APPEND ls_table1 TO lt_table1.
ENDSELECT
Here you append to ls_table1 only those rows where count is equals to 5 in selection of table2.
Hope it helps.

find space in string postgresql

How to find space after words in postgresql:
I have two same strings in my database :
string1
string1
I am trying to find the one with 2 spaces before and one space after.
here are some of the queries I used with their results:
SELECT * FROM table WHERE "column" LIKE '__string1_'; --> *no result*
SELECT * FROM part1 WHERE "column" LIKE '__string1%';
Results:
1) string1 and xyx
2) string1 and string2
3) string1
but I only need string1 with no strings after or before.
There are likely several ways to accomplish this. See PostgreSQL's pattern matching documentation for some examples.
However, I use % to find patterns: select * from table where column ILIKE '%string1%'; would return anything with string1 in it, including the cols with spaces.
You can also try escaping the spaces: select * from table where column ILIKE '\ \ string1\ ';
or, even simpler select * from table where column ILIKE ' string1';
I also use the case insensitive ILIKE as an alternative for a case sensitive LIKE, so case will not matter in your query.
I will just complement the above answer:
Suppose you want to find any space in the column Name in the demo table, then the code would be like:
SELECT * FROM demo
WHERE Name LIKE '% %'
ORDER BY Name
Now, if you want any string 'a' for example inside the column, you would just have the following:
SELECT * FROM demo
WHERE Name LIKE '%a%'
ORDER BY Name
and also, for words that begin with a space, you would just use '_' (in a certain position):
SELECT * FROM demo
WHERE Name LIKE '_a%'
ORDER BY Name

How to order results based on number of search term matches?

I am using the following InnoDB tables in mysql to describe records that can have multiple searchtags associated with them:
TABLE records
ID
title
desc
TABLE searchTags
ID
name
TABLE recordSearchTags
recordID
searchTagID
To SELECT records based on arbitrary search input, I have a statement that looks sort of like this:
SELECT
recordSearchTags.recordID
FROM
recordSearchTags
LEFT JOIN searchTags
ON recordSearchTags.searchTagID = searchTags.ID
WHERE
searchTags.name LIKE CONCAT('%','$search1','%') OR
searchTags.name LIKE CONCAT('%','$search2','%') OR
searchTags.name LIKE CONCAT('%','$search3','%') OR
searchTags.name LIKE CONCAT('%','$search4','%');
I'd like to ORDER this resultset, so that rows that match with more search terms are displayed in front of rows that match with fewer search terms.
For example, if a row matches all 4 search terms, it will be top of the list. A row that matches only 2 search terms will be somewhere in the middle. And a row that matches just one search term will be at the end.
Any suggestions on what is the best way to do this?
Thanks!
* Replaced answer, since fulltext isn't an option
Alright, it's not pretty, but you should be able to do something like this:
ORDER BY (searchTags.name LIKE CONCAT('%','$search1','%')
+ searchTags.name LIKE CONCAT('%','$search2','%')
+ searchTags.name LIKE CONCAT('%','$search3','%')
+ searchTags.name LIKE CONCAT('%','$search4','%'))
DESC;
LIKE returns 1 on a match or 0 if there is no match, so you should just be able to add the results together.
This isn't very pretty but one way would be to union the 4 likes in 4 statements like
select ... where searchTags.name LIKE CONCAT('%','$search1','%')
union
select ...
and so on. Wrap that in a:
select recordSearchTags.recordID, count(*) from (<inner unions>)
group by recordSearchTags.recordID
order by count(*)

Resources