Extract word by Regex postgreSQL [duplicate] - sql

This question already has an answer here:
Postgresql regexp_matches syntax not working as expected
(1 answer)
Closed 2 years ago.
a colums include code like 'a357' , 'b123' with many word and i want to extract them
it work with
select *, regexp_matches(col1,'a\d{3}') from table
but i also want the 'b123' code then i write this code not work:
select *, regexp_matches(col1,'(a|b)\d{3}') from table
where as (a|b) is regex. Please show me solution or any other way not regexp_matches because i need to trim '{}' sign after that.

The issue is the subexpression in parentheses. As the documentation explains:
If a match is found, and the pattern contains parenthesized
subexpressions, then the result is a text array whose n'th element is
the substring matching the n'th parenthesized subexpression of the
pattern.
In your case, this is easily fixed by using a character class:
regexp_matches(col1, '[ab]\d{3}')

Related

Explanation in REGEXP_REPLACE function output [duplicate]

This question already has answers here:
How to capture multiple repeated groups?
(11 answers)
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Following two query gives some output,
Select REGEXP_REPLACE('Milind,Milind,Gopal,Gopal,Gopal,Milind'), '([^,]+) (,\1)+(,|$)', '\1\3') "OUTPUT"
FROM dual;
o/p Milind,Gopal,Milind
Select REGEXP_REPLACE('Milind,Milind,Gopal,Gopal,Gopal,Milind'), '([^,]+) (,\1+)(,|$)', '\1\3') "OUTPUT"
FROM dual;
o/p Milind,Gopal,Gopal,Milind
I do not understand the workflow. How it is giving output.
Can anyone help me out and explain.
[^,] matches one character, that can be anything except a comma. For example: M.
[^,]+ matches 1 or more times any character except a comma. For example: Milind
([^,]+) same, but capture the result, so it can be referenced later with \1.
(,\1)+ matches a comma followed by the previous capture, one or more times, and capture the result as \2. If \1 is Milind, it can match: ,Milind, or ,Milind,Milind or ,Milind,Milind,Milind, etc.
(,|$) matches either a comma or the end of the line, and captures it as \3.
\1\3 This is the replacement pattern: we are only keeping \1 and \3, so everything matched in capture \2 is effectively removed.
The second statement is showing a small difference that introduces a bug:
(,\1+) matches a comma followed by one or more occurences of the previous capture, as in: ,Milind, or ,MilindMilind or ,MilindMilindMilind. As a result, it fails to remove several comma separated occurences.

SQL Server: select the first character that is not a digit 0-9 or / [duplicate]

This question already has answers here:
SQL to find first non-numeric character in a string
(3 answers)
Closed 5 years ago.
I have a table called Person, and a NVarChar column called Notes.
The Notes column has a lot of text in it, but always begins with a number of some kind, with /'s inserted throughout.
For example:
1/23 some text
45/678/9%*&^%$##
02/468/ some other text
I need to select the first character position that isn't a digit or /.
I don't care whether the position is 0-based or 1-based; I can accommodate that after the fact.
In this example, if I'm using 1-based character positions, the selection should produce the following:
5
9
8
So you're looking for an index that matches some sort of pattern, say a pattern index. If we're whimsical, we might abbreviate it to PATINDEX.
SELECT PATINDEX('%[^0-9/]%', Notes)
FROM Person

NOT LIKE IN statement in SQL [duplicate]

This question already has answers here:
Is there a combination of "LIKE" and "IN" in SQL?
(28 answers)
Closed 5 years ago.
I need to print names that do not start and end with vowel. I tried it like this:
SELECT DISTINCT name FROM people WHERE
lower(name) NOT LIKE IN ('a','e','i','o','u')%('a','e','i','o','u');
I got error.
You may want to try avoid using REGEXP from performance reasons in case of large data sets.
In such case is TRANSLATE your friend.
1) translate all vowels to one representative
2) perform normal LIKE predicate with the selected vowel
select txt from tab1
where translate(lower(txt),'aeiou','aaaaa') not like 'a%a';
REGEXPs are mighty, but should not be used on non-trivial data sets in case that they could be avoided. (My 8M rows test data gives 7 seconds elapsed using TRANSLATE vs. 2+ minutes with REGEXP).
You can use regexp_like with match parameter i for case insensitive matching:
select distinct name from people
where not regexp_like(name, '^[aeiou]($|.*[aeiou]$)', 'i');
Pattern details:
^[aeiou] - starts with a vowel
($|.*[aeiou]$) - either there is only one character (matched in the first step) or ends with a vowel.

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.

How to make column values more presentable [duplicate]

This question already has answers here:
How to get rightmost 10 places of a string in oracle
(5 answers)
Closed 8 years ago.
I have a table containing the following fields:
version
id
set_value
marker
I want to write a SELECT statement to query them. However, the values in the column marker are not easily readable. I would like to present a substring of that column. My question is how do I do this?
You can use this:
SELECT version,
id,
set_value,
SUBSTR(marker, 1, 10) AS marker
FROM ...
to select just the first ten characters of marker, and still have the resulting column be named "marker".
(See http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions169.htm.)
You can use the substr function.