Containstable is not working for special characters - sql

I have a table, as an example Fruits, with column 'Name' which is Full Text Indexed
| ID | Name |
| 0 | *Apple |
| 1 | *Banana|
If we use full text search
select * from CONTAINSTABLE (Fruits, Name , '*')
This is ignored, as an escape character. While I have tried to use '/' and '%' to search but no use. Is there any option to search special characters?
I don't want to use LIKE Operator as it more time than Full text search.

Related

How do I search "%" (percentage sign) in text using ILIKE operator in PostgreSQL [duplicate]

This question already has answers here:
How can you find a literal percent sign (%) in PostgreSQL using a LIKE query?
(3 answers)
Closed 2 years ago.
| id | text |
|----|-------|
| 1 | AB |
| 2 | CD%EF |
| 3 | GH |
I have a text column in a table having a value with a "%" sign.
I wanted to extract that value using the following query —
SELECT text FORM table
WHERE text ILIKE '%%%'
expected Output should be:
CD%EF
1 {row}
actual output returns:
AB
CD%EF
GH
3 {rows}
You can use \% to represent a literal percent sign:
SELECT text FORM table WHERE text ILIKE '%\%%'
Like the documentation says:
To match a literal underscore or percent sign without matching other characters, the respective character in pattern must be preceded by the escape character. The default escape character is the backslash but a different one can be selected by using the ESCAPE clause. To match the escape character itself, write two escape characters.

Only return fields that contain numbers or special characters EXCEPT . Error

In Redshift I want to return fields that contain numbers or special characters EXCEPT . (anything other and a-z and A-Z)
The following gets me anything that contains a number but I need to extend this to any special character except full stop (.)
SELECT DISTINCT name
FROM table
WHERE name ~ '[0-9]'
I need something like:
SELECT DISTINCT name
FROM table
WHERE name ~ '[0-9]' OR name ~'[,#';:#~[]{}etcetc'
Sample Data:
name
john
joh1n1
j!ohn!
jo!h2n
joh.n
jo.&hn
j.3ohn
j.$9ohn
Expected Output:
name
joh1n1
j!ohn!
jo!h2n
jo.&hn
j.3ohn
j.$9ohn
You may use
WHERE name !~ '^[[:alpha:].]+$'
Here, all records that do not consist of only alphabetic or dot symbols will be returned. ^ matches the start of a string position, [[:alpha:].]+ matches one or more letters or dots and $ matches the end of string position.
If it is for PostgreSQL you may use
WHERE name SIMILAR TO '%[^[:alpha:].]%'
The SIMILAR TO operator accepts POSIX character classes and bracket expressions and wildcards, too, and requires a full string match. So, % allows any chars before any 1 char other than letter or dot ([^[:alpha:].]), and then there may also be any other chars till the end of the string.
You can do:
SELECT DISTINCT name FROM table WHERE name !~* '[a-z]'
This means: match on names that do not contain any alphanumeric character.
Operator !~* means:
Does not match regular expression, case insensitive
Edit based on the provided sample data and expected results.
If you want to match on names that contain at least one character other than an alphabetic character or a dot, then you can do:
select * from mytable where name ~* '[^a-z.]'
Demo on DB Fiddle:
with mytable(name) as (values
('john'),
('joh1n1'),
('j!ohn!'),
('jo!h2n'),
('joh.n'),
('jo.&hn'),
('j.3ohn'),
('j.$9ohn')
)
select * from mytable where name ~* '[^a-z.]'
| name |
| :------ |
| joh1n1 |
| j!ohn! |
| jo!h2n |
| jo.&hn |
| j.3ohn |
| j.$9ohn |

Query to search substring in column

I have a table that has a substring value in the column and I want to write a query that checks if input string has the substring.
My table looks like:
| company | host |
| ------- | ---------- |
| ebay | ebay.com |
| google | google.com |
| yahoo | yahoo.com |
My input will be like www.ebay.com or https://www.ebay.com or www.qa.ebay.com or www.dev.ebay.com..
If I get any of the inputs I want to return the first record.
I tried looking at the CHARINDEX, INSTR but they are work in reverse. My scenario is I have substring to be searched in table and the actual string as input.
Any help is appreciated.
You can use like for this, but you also need string concatenation. In ANSI standard SQL, this looks like:
select t.*
from t
where #inputstring like concat('%.', t.host)
where #inputstring is the string you are inputting.
Note: You can also use the concatenation infix operation, which is typically || (standard) or +.
You can use the SQL wildcard like so:
SELECT * FROM table WHERE host LIKE '%ebay.com';
Go for this:
SELECT * FROM table WHERE host LIKE '%SearchString%'
It will pull all rows containing the SearchString.
You can achieve this using like operator.
Select * from yourtable
where ? like concat('%', company, '%');
parameter ? with your input.

Wildcard of Number in SQL Server

How to match numbers in SQL Server 'LIKE'.
SpaceName
------------
| New_Space_1
| .
| .
| New_Space_8
| New_Space_9
| New_Space_10
| New_Space_11
| New_Space_SomeString
| New_Space_SomeString1
Above is my table contents.
I want to get only records ending with Numeric chars, ie I want the records from New_Space_1 to New_Space_11.
Don't want New_Space_SomeString and New_Space_SomeString1
I have some query like this.
SELECT SpaceName FROM SpaceTable
WHERE SpaceName LIKE 'New_Space_%'
But this returns all records.
what about
SELECT SpaceName FROM SpaceTable
WHERE SpaceName LIKE 'New[_]Space[_][0-9]%'
The reason I put underscore in brackets is because in a regular expression _ means Any single character. Read up on like here http://msdn.microsoft.com/en-us/library/ms179859.aspx
This solution from #SteveKass works perfect.
SELECT SpaceName FROM SpaceTable WHERE SpaceName LIKE 'New[_]Space[_]%' AND SpaceName NOT LIKE 'New[_]Space[_]%[^0-9]%'

PostgreSQL String search for partial patterns removing exrtaneous characters

Looking for a simple SQL (PostgreSQL) regular expression or similar solution (maybe soundex) that will allow a flexible search. So that dashes, spaces and such are omitted during the search. As part of the search and only the raw characters are searched in the table.:
Currently using:
SELECT * FROM Productions WHERE part_no ~* '%search_term%'
If user types UTR-1 it fails to bring up UTR1 or UTR 1 stored in the database.
But the matches do not happen when a part_no has a dash and the user omits this character (or vice versa)
EXAMPLE search for part UTR-1 should find all matches below.
UTR1
UTR --1
UTR 1
any suggestions...
You may well find the offical, built-in (from 8.3 at least) fulltext search capabilities in postrgesql worth looking at:
http://www.postgresql.org/docs/8.3/static/textsearch.html
For example:
It is possible for the parser to produce overlapping tokens from the
same of text.
As an example, a hyphenated word will be reported both as the entire word
and as each component:
SELECT alias, description, token FROM ts_debug('foo-bar-beta1');
alias | description | token
-----------------+------------------------------------------+---------------
numhword | Hyphenated word, letters and digits | foo-bar-beta1
hword_asciipart | Hyphenated word part, all ASCII | foo
blank | Space symbols | -
hword_asciipart | Hyphenated word part, all ASCII | bar
blank | Space symbols | -
hword_numpart | Hyphenated word part, letters and digits | beta1
SELECT *
FROM Productions
WHERE REGEXP_REPLACE(part_no, '[^[:alnum:]]', '')
= REGEXP_REPLACE('UTR-1', '[^[:alnum:]]', '')
Create an index on REGEXP_REPLACE(part_no, '[^[:alnum:]]', '') for this to work fast.