This question already has answers here:
Split String by delimiter position using oracle SQL
(2 answers)
Closed last month.
I have a select query in Postgres with SPLIT_PART() function which works fine in postgres
SELECT SPLIT_PART(totalscore, '_', 1) highlow,
SPLIT_PART(totalscore, '_', 2) score,
score_desc
FROM student_score
WHERE (totalscore LIKE 'HN%' OR totalscore LIKE 'LN%')
http://sqlfiddle.com/#!15/877966/4
The SPLIT_PART() is not working in Oracle
Could please let me know how to achieve the same in Oracle as SPLIT_PART() is not working .
http://sqlfiddle.com/#!4/042f62/5
You can use REGEXP_SUBSTR() with [^_]+ patterns such as
SELECT REGEXP_SUBSTR(totalscore, '[^_]+') AS highlow,
REGEXP_SUBSTR(totalscore, '[^_]+$') AS score,
score_desc
FROM student_score
WHERE REGEXP_LIKE(totalscore,'^HN_|^LN_')
Demo
considering the whole dataset contains one and only one underscore per each value of the column.
Related
This question already has answers here:
Oracle REGEXP_SUBSTR | Fetch string between two delimiters
(2 answers)
Closed 2 years ago.
I want to retrieve a String between two characters.
I have whole string like "Attachments:Attachments~Attachment" and I want to take substring between : and ~ characters that is output will be Attachments.
How can be this done in SQL/Oracle select statement?
You can use the REGEXP_SUBSTR function. Starting with Oracle 11g, there is a parameter to the function, which lets you specify the capture group that you want returned:
SELECT regexp_substr('Attachments:Attachments~Attachment', '\:(.+)\~', 1,1,NULL,1) from dual;
There are workarounds for older versions (see also https://stackoverflow.com/a/7759146/14015737). You can shorten your result:
SELECT rtrim(ltrim(regexp_substr('Attachments:Attachments~Attachment', '\:(.+)\~'),':'), '~') FROM dual;
or
SELECT substr( match, 2, length(match)-2 ) from (
SELECT regexp_substr('Attachments:Attachments~Attachment', '\:(.+)\~') match FROM dual);
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 '%[%]%'
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.
This question already has answers here:
Split function equivalent in T-SQL?
(16 answers)
Closed 10 years ago.
I need to populate columns in my database for Latitude and Longitude, however the original information is stored as a single string
eg.
UDFChar1 = 41.243223,-8.183913
I am guessing that the TRIM command will come in useful here, but I do not know how I can tell it to stop exactly on the comma for each half.
What I'm hoping to be able to come up with is a simple UPDATE query as per the below:
UPDATE Asset
SET Lattitude = (SELECT LTRIM(UDFChar1)),
Longitude = (SELECT RTRIM(UDFChar1))
but obviously with some extra work in the LTRIM and RTRIM parts so that I am only selecting the data up to, and not including the comma in UDFChar1
Any ideas on how to achieve this?
Please try:
left(Col, charindex(',', Col)-1)
and
right(Col, len(Col)-charindex(',', Col))
sample
SELECT
LEFT(COL, CHARINDEX(',', Col)-1) Lattitude,
RIGHT(COL, LEN(COL)-CHARINDEX(',', Col)) Longitude
FROM(
SELECT '41.243223,-8.183913' Col
)x
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.