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.
Related
Is it possible to write a query which searches for non-space, non-letter and non-digit characters in a column? I need to check if there are any chars like minus, apostrophes and so on.
SELECT * FROM MyTable WHERE MyCol "has at least one letter which is not A-Z, a-z or 0-9";
You can't really do this easily without regex support. If your version of SQLite supports REGEXP, then it provides one way:
SELECT *
FROM MyTable
WHERE MyCol REGEXP '[^A-Za-z0-9 ]';
I have the following SQL-Query
It is comparing the item Name to the Name of the Project to check if it overlaps.
Not if i have a single quote ' in the Project or SubProject my query fails.
How can i escape the singe quote in my LIKE?
My Databases Look like this
TItems
Name
-------
testitem
te'stitem
TProject
Project
--------
test
te'st
My SQL query looks like this
SELECT * FROM TItems AS items
LEFT JOIN TProject AS projs
ON LOWER(items.Name) LIKE '%'||LOWER(projs.Project)||'%'
The result should be
Name | Project
testitem | test
te'stsitem | te'st
however it chrashe because i have a single quote in my likes. How can I escape them. These are values from the db.
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 ....
I'm wondering what constraints SQLite puts on table and column names when creating a table. The documentation for creating a table says that a table name can't begin with "sqlite_" but what other restrictions are there? Is there a formal definition anywhere of what is valid?
SQLite seems surprisingly accepting as long as the name is quoted. For example...
sqlite> create table 'name with spaces, punctuation & $pecial characters?'(x int);
sqlite> .tables
name with spaces, punctuation & $pecial characters?
If you use brackets or quotes you can use any name and there is no restriction :
create table [--This is a_valid.table+name!?] (x int);
But table names that don't have brackets around them should be any alphanumeric combination that doesn't start with a digit and does not contain any spaces.
You can use underline and $ but you can not use symbols like: + - ? ! * # % ^ & # = / \ : " '
From the sqlite doc,
If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite:
'keyword' A keyword in single quotes is a string literal.
"keyword" A keyword in double-quotes is an identifier.
[keyword] A keyword enclosed in square brackets is an identifier. This is not standard SQL. This quoting mechanism is used by MS Access and SQL Server and is included in SQLite for compatibility.
`keyword` A keyword enclosed in grave accents (ASCII code 96) is an identifier. This is not standard SQL. This quoting mechanism is used by MySQL and is included in SQLite for compatibility.
So, double quoting the table name and you can use any chars. [tablename] can be used but not a standard SQL.
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.