Return everything after specific value [duplicate] - sql

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

Related

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.

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.

How to replace characters in SQL [duplicate]

This question already has an answer here:
Converting special character to plain text in oracle
(1 answer)
Closed 5 years ago.
I'm a new SQL user, and I'd like to know if there is a simple way of replacing non-English characters in Oracle SQl-developer. Let's Say, I want Hernán Nuñez to show up as Hernan Nunez, but without having to actually replace "Hernán" with "Hernan". What I need is to replace everything that contains "á" to the same thing, but with "a" instead, for example. Any suggestions?
US7ASCII will give you the right result. Example:
SELECT CONVERT('BESANÇON','US7ASCII')
FROM table;
CONVERT(
--------
BESANCON
1 row selected.

Sql Server - Replace only the first characters in a string [duplicate]

This question already has answers here:
Removing leading zeroes from a field in a SQL statement
(15 answers)
Closed 9 years ago.
How can I replace only the the first zeros in a string?
Example:
string : '0000089001232100'
result: '89001232100'
I can't use SQL Server REPLACE function since I don't want all my zeros to be replaced.
Thanks
select substring(ColumnName, patindex('%[^0]%',ColumnName), 10) should give you what you need.
Also look into the RIGHT and LEFT functions. They strip a string from the left or right side, respectively.
If you wanted to use RIGHT, you could get the index of the first 'wanted' character using CHARINDEX(http://msdn.microsoft.com/en-us/library/ms186323.aspx), then use that index as the point to strip from in your RIGHT function.
If the string always have only numbers, you could use this:
DECLARE #String VARCHAR(30)
SET #String = '0000089001232100'
SELECT CONVERT(VARCHAR(20),CONVERT(NUMERIC(20,0),#String))

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.