How to escape single quotes in Oracle? [duplicate] - sql

This question already has answers here:
How to handle a single quote in Oracle SQL
(2 answers)
Closed 7 years ago.
I have a column containing certain expressions stored as a text string which include single quotatons, such as 'missed transaction' (INCLUDING the quotations)
How can I use a where clause with such an occurance?
select * from table where reason = ''missed transaction''
doesn't work, and I can't use the replace function because it also requires single quotations in its syntax. Obscure problem, i know. But thanks for any help.

You need to escape the ' by doubling them :
select * from table where reason = '''missed transaction''';

The q quote syntax makes this sort of thing easier:
select * from table where reason = q'['missed transaction']'
Everything between the '[ and the ]' is interpreted literally, so no need to double all the quotes, however many there may be.

Related

SQL Select syntax [duplicate]

This question already has answers here:
What is the use of the square brackets [] in sql statements?
(10 answers)
What does the colon sign ":" do in a SQL query?
(8 answers)
Closed 2 years ago.
I've came cross a SQL SELECT statement which is different than what I normally see:
Select Distinct [Employee: Department_key_historic],[Employee: Department_key_current]
From [shared].[vw_Table]
What does the colon do in the above code? I am used to see format like Select table.columnName but never a :
This appears to be a SQL Server query. If that is the case, [] are used to surround object names. Therefore the : that you see are simply part of the name of the column itself.

How to search for presence of em dash in T-SQL? [duplicate]

This question already has answers here:
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 2 years ago.
I have a database column that is nvarchar(50). There are some records that might have an em dash in them.
I want to find those records.
However, when I copy and paste an em-dash from another program, SQL Server Management Studio treats it like a regular hyphen.
This just returns all the parts with hyphens even though it's an em-dash in between the percent signs:
select * from part
where partnum like '%−%'
How can I make SSMS search for the em-dash and not hyphens?
emdash: −
hyphen: -
In case anyone is wondering this was solved by learning how to use NVARCHAR searches. You can search for something you copy paste from another program by prefixing the search string with an 'N' like this:
SELECT * FROM part
WHERE PartNum LIKE N'%−%'
You could try something like this.
select * from part
where partnum like '%' + NCHAR(8212) + '%'

Finding a record with apostrophe using LIKE [duplicate]

This question already has answers here:
PL/SQL, how to escape single quote in a string?
(5 answers)
How to anticipate and escape single quote ' in oracle
(2 answers)
How to handle a single quote in Oracle SQL
(2 answers)
Closed 4 years ago.
I need to find a below record
O'GRAD
I wanted to use LIKE
LIKE 'O'GRAD'
But i come across a problem with apostrophe.
What would be the best way around it?
Double up the apostrophe
LIKE 'O''GRAD'
or use the Q syntax
LIKE q'{O'GRAD}'
if your Oracle DB version is 10g or upper you may use :
select *
from mytable t
where t.col1 like '%'||q'$O'GRAD$'||'%';
/
or classically add an extra quote to existing quote
select *
from mytable t
where like '%'||'O''GRAD'||'%';
/
to overcome the single quotation mark problem.

Return everything after specific value [duplicate]

This question already has an answer here:
Convert fraction to decimal [closed]
(1 answer)
Closed 9 years ago.
Working on 10g.
I tried to write a regexp that removes everything before the first dash(-).
However, I did not anticipate the complexity of the syntax.
I have this:
REGEXP_SUBSTR (myVal, '[^"]+')
This removes all the values after the first double quotation leaving me with data like:
10-3/4, 5-1/2, 7-5/8
I assumed that changing the double quotes to a dash and putting the carat after the dash would do it, but no luck.
If that's all you need to do, then you don't need to use REGEX, since you could just do it with other simpler functions:
SELECT SUBSTRING(myVal,INSTR(myVal,'-'),LENGTH(myVal))
FROM YourTable
If you want to do that with REGEXP_SUBSTR, try this:
SELECT REGEXP_SUBSTR(myValue, '\-.*$')
FROM myTable
But the SUBSTR answer from Lamak is just as effective. The third SUBSTR parameter defaults to the length of the string so you can leave it off:
SELECT SUBSTR(myValue, INSTR(myValue, '-'))
FROM myTable

How to escape square bracket when using LIKE? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL Server LIKE containing bracket characters
I am having a problem with pattern matching.I have created two objects say,with codes
1)[blah1]
2)[blah2] respectively
in the search tab,suppose if i give "[blah" as the pattern,its returning all the strings
i.e., [blah1] and [blah2]
The query is
select *
from table1
where code like N'%[blah%'
I guess the problem is with the condition and special characters. Please do revert if you have as solution. Is there any solution where we can escape the character"[". I tried to change the condition as N'%[[blah%'.But even then its returning all the objects that is in the table.
When you don't close the square bracket, the result is not specified.
However, the story is different when you close the bracket, i.e.
select *
from table1
where code like N'%[blah%]%'
In this case, it becomes a match for (any) + any of ('b','l','a','h','%') + (any). For SQL Server, you can escape characters using the ESCAPE clause.
select * from table1 where code like N'%\[blah%\]%' escape '\'
SQL Fiddle with examples
You can escape a literal bracket character this way:
select *
from table1
where code like N'%[[]blah%'
Source: LIKE (Transact-SQL), under the section "Using Wildcard Characters As Literals."
I guess this is Microsoft's way of being consistent, since they use brackets to delimit table and column identifiers too. But the use of brackets is not standard SQL. For that matter, bracket as a metacharacter in LIKE patterns is not standard SQL either, so it's not necessary to escape it at all in other brands of database.
As per My understanding, the symbol '[', there is no effect in query. like if you query with symbol and without symbol it shows same result.
Either you can skip the unwanted character at UI Level.
select * from table1 where code like '%[blah%'
select * from table1 where code like '%blah%'
Both shows same result.