Search "[" or "]" inside a table using SQL command - asp.net

In an SQL table I have following columns
id, title, content
And I would like to search text inside title or content having "[" or "]".
What will be the SQL command to fetch such kind of data?
(edit: removed "efficient")
(added)
As per "rsbarro"
I have tried and my findings are:
If I try for
SELECT * FROM MyTable WHERE Title LIKE '%[%'
returns 0 row.
If I try for
SELECT * FROM MyTable WHERE Title LIKE '%]%'
returns number of rows.

Try using the LIKE operator, but remember if you are searching for a [ you need to escape it as [[] (see #billinkc's answer for an explanation on the escaping, or check the MSDN documentation and look for the section "Using Wildcard Characters As Literals"). The statement:
SELECT * FROM MyTable WHERE Title LIKE '%[[]%'
will search for any record in MyTable where Title contains a [. The % is a wildcard character.
I'm not exactly sure from the question what you are trying to find, but if you want to search for a title containing both brackets (i.e., '??[match]??'), try:
SELECT * FROM MyTable WHERE Title LIKE '%[[]%]%'
EDIT
If you are trying to match either [ or ] in title or in content, the query would be:
SELECT * FROM MyTable
WHERE
Content LIKE '%[[]%' OR Content LIKE '%]%' OR
Title LIKE '%[[]%' OR Title LIKE '%]%'
This query would probably not be terribly efficient if you have a table with a large number of rows.

The examples above are failing for like matching because LIKE offers a single character match option with [].
Any single character within the specified range ([a-f]) or set ([abcdef]).
To actually match a [, you will need to embed it in a pair of brackets
;
WITH DEMO (Title) AS
(
SELECT 'Where are you [ I am here]'
UNION ALL SELECT 'WHO ARE YOU'
UNION ALL SELECT 'asdafasfa][asdfadfasdfasdfa'
)
SELECT
D.*
FROM
DEMO D
WHERE
D.Title LIKE '%[[]%'
OR D.Title LIKE '%[]]%'
Returns
Where are you [ I am here]
asdafasfa][asdfadfasdfasdfa

In Sql [ is a special character and where [[] means [, so the below Sql query will work accordingly.
SELECT * FROM MyTable WHERE (Title LIKE '%[[]%' OR content LIKE '%[[]%')
OR (Title LIKE '%]%' OR content LIKE '%]%')
Please see herefor more details.

Related

SQlite full-text search MATCH miltiple columns with OR and AND

I am learning how to use FTS4 in SQlite and trying to translate something like this:
SELECT * FROM table
WHERE (postal_code LIKE q1 OR place_name LIKE q1) AND (address LIKE q2)
which works perfectly with the normal table, to something like that:
SELECT * FROM table
WHERE table MATCH '(postal_code: q1 OR place_name: q1) AND (address: q2)'
which is not working with the same table converted to VIRTUAL .. USING fts4...
Searching for match in specific column with ":" is working, but how can I combine this with OR and AND?
Background - I try to work with a flexible query, where q1 can be one of the both: postal-code or place name and q2 has to be address.
EDIT:
This is what I got so far (partially) working
SELECT * FROM table WHERE table MATCH ('postal_code:q1 OR place_name:q1')
INTERSECT
SELECT * FROM table WHERE table MATCH ('address:q2');
(building on this post from #CL)
The first problem here are words with space - if I search for something like
...MATCH ('postal_code:"new haven*" OR place_name:"new_haven*"')
then I have "malformed MATCH expression"
The second problem is when I have empty query, like
...MATCH ('*')
then I get nothing

How to query Unicode characters from SQL Server 2008

With NVARCHAR data type, I store my local language text in a column. I face a problem how to query that value from the database.
ዜናገብርኤልስ is stored value.
I wrote SQL like this
select DivisionName
from t_Et_Divisions
where DivisionName = 'ዜናገብርኤልስ'
select unicode (DivisionName)
from t_Et_Divisions
where DivisionName = 'ዜናገብርኤልስ'
The above didn't work. Does anyone have any ideas how to fix it?
Thanks!
You need to prefix your Unicode string literals with a N:
select DivisionName
from t_Et_Divisions
where DivisionName = N'ዜናገብርኤልስ'
This N prefix tells SQL Server to treat this string literal as a Unicode string and not convert it to a non-Unicode string (as it will if you omit the N prefix).
Update:
I still fail to understand what is not working according to you....
I tried setting up a table with an NVARCHAR column, and if I select, I get back that one, exact row match - as expected:
DECLARE #test TABLE (DivisionName NVARCHAR(100))
INSERT INTO #test (DivisionName)
VALUES (N'ዜናገብርኤልስ'), (N'ዜናገብርኤልስ,ኔትዎርክ,ከስተመር ስርቪስ'), (N'ኔትዎርክ,ከስተመር ስርቪስ')
SELECT *
FROM #test
WHERE DivisionName = N'ዜናገብርኤልስ'
This returns exactly one row - what else are you seeing, or what else are you expecting??
Update #2:
Ah - I see - the columns contains multiple, comma-separated values - which is a horrible design mistake to begin with..... (violates first normal form of database design - don't do it!!)
And then you want to select all rows that contain that search term - but only display the search term itself, not the whole DivisionName column? Seems rather pointless..... try this:
select N'ዜናገብርኤልስ'
from t_Et_Divisions
where DivisionName LIKE N'%ዜናገብርኤልስ%'
The LIKE searches for rows that contain that value, and since you already know what you want to display, just put that value into the SELECT list ....

How to search for single quote in Oracle11g

How can I search for a name like O'Neil from a table when I use a query like
select * from table_name where name like 'O'Neil';
then it shows an error.
Escape it with the second single quote:
select * from table_name where name like 'O''Neil';
Since Oracle 10g there is also a quote-operator:
select * from table_name where name like q'('O'Neil)';
Syntax: q'c text-to-be-quoted c'. c is a single character (called the quote delimiter). With the «quote operator» apostrophes don't have to be doubled.

SQLite: How to select part of string?

There is table column containing file names: image1.jpg, image12.png, script.php, .htaccess,...
I need to select the file extentions only. I would prefer to do that way:
SELECT DISTINCT SUBSTR(column,INSTR('.',column)+1) FROM table
but INSTR isn't supported in my version of SQLite.
Is there way to realize it without using INSTR function?
below is the query (Tested and verified)
for selecting the file extentions only. Your filename can contain any number of . charenters - still it will work
select distinct replace(column_name, rtrim(column_name,
replace(column_name, '.', '' ) ), '') from table_name;
column_name is the name of column where you have the file names(filenames can have multiple .'s
table_name is the name of your table
Try the ltrim(X, Y) function, thats what the doc says:
The ltrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the left side of X.
List all the alphabet as the second argument, something like
SELECT ltrim(column, "abcd...xyz1234567890") From T
that should remove all the characters from left up until .. If you need the extension without the dot then use SUBSTR on it. Of course this means that filenames may not contain more that one dot.
But I think it is way easier and safer to extract the extension in the code which executes the query.

Counting words in a sqlite FTS4

I have a sqlite3 full text search table defined like this:
CREATE VIRTUAL TABLE entries USING fts4 ( entry TEXT )
Each entry row has a line of text. How can I write a query to count the total amount of words in the table? Thanks
I don't know of a built-in function to do that, but you could re-use the answer to
" Query to count words SQLite 3 " to get the total count of words:
select sum(length(trim(entry))
- length(replace(trim(entry), ' ', '')) + 1) from entries;
(Modified the original answer by adding the trim.)
If you have sqlite3 version 3.7.6 or later, you can do something cleaner with an fts4aux table.
create virtual table terms using fts4aux(entries);
select count(distinct term) from terms;

Resources