Finding a record with apostrophe using LIKE [duplicate] - sql

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.

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 % using SQL and Wildcards? [duplicate]

This question already has answers here:
How do I escape a percentage sign in T-SQL?
(5 answers)
Closed 3 years ago.
I have to replace % in a number of fields. I need to get a list of the records to be changed first. I know how to do the actual REPLACE easily enough, but my query to find the records isn't working correctly.
SELECT * FROM inventory WHERE desc LIKE '%%%'
I also tried the following the the same results:
SELECT * FROM inventory WHERE desc LIKE '%'+CHAR(37)+'%'
What's the best way to search for %?
I am using SQL Server 2016.
You need to escape the wildcard:
where [desc] like '%$%%' escape '$'
or, use a character class:
where [desc] like '%[%]%'

How to escape single quotes in Oracle? [duplicate]

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.

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

Split Up Text In A Column [duplicate]

This question already has answers here:
How to split string using delimiter char using T-SQL?
(4 answers)
How to split a comma-separated value to columns
(38 answers)
Split values over multiple rows [duplicate]
(2 answers)
Closed 9 years ago.
I have this piece of text that is stored in our MS-SQL database (ignore the quotes and no, I can't redesign how this work specifically):
"TEST|00000298398293|EQ5|Patient"
Now, when I do a simple select, I get that result being returned. What I'd like to do is split that string based on the "|" character and return the individual strings associated with this string, so that I could have "TEST", "0000298398293", "EQ5" and "Patient" in different fields. How can I do this? In PHP, you can use the explode method, is there something like that in MS-SQL?
It's surely not the most elegant solution but i've used in in the past:
DECLARE #Sql varchar(50) = 'TEST|00000298398293|EQ5|Patient'
SELECT
PARSENAME(REPLACE(#sql,'|','.'),4),
PARSENAME(REPLACE(#sql,'|','.'),3),
PARSENAME(REPLACE(#sql,'|','.'),2),
PARSENAME(REPLACE(#sql,'|','.'),1)
Notice : This only works if you have 3 pipes, eventually consider to redesign your database in the future!