regexp_substr get last two words from end of the sentence in Oracle SQL - regexp-substr

I have a string: ON P6B 0B8. The output I need is: P6B OB8.
I can use regexp_substr('ON P6B 0B8','[^ ]+$',1) to get the last word from the end of the sentence. But how would I get the word after the spaceā€”the second word from the end?
How do I tell regexp_substr to not stop at the first space when looking from behind, and instead move on until it hits the second space?
I had a tough time understanding the metacharacters provided by Oracle regexp.

Here's a regex that will get the last 2 sets of characters from your string. Since it appears you are getting a Canadian postcode though you may want to be a little more careful.
The WITH clause sets up a table with data. Notice the first row is a valid postcode format, but the second row is bad (2 letters in a row). Always use unexpected data for your test cases, you don't want any surprises and the data WILL always contain surprises.
The first regex matches 2 sets of 3 characters separated by a space at the end of the string. At first glance this may seem OK but if the data is bad it will get returned. To tighten it up, use the second regex, which specifically checks for the Canadian postcode format of uppercase_letter-digit-uppercase_letter-space-digit-uppercase_letter-digit and will return NULL if it is not found. Maybe you want to catch this with a NVL() call and return a message instead.
with tbl(str) as (
select 'Windsor ON P6B 0B8' from dual union all
select 'Windsor_bad_postcode ON A3C 9BB' from dual
)
select --regexp_substr(str, '.* (.{3} .{3})$', 1, 1, NULL, 1) postcode_w_bad
regexp_substr(str, '.* ([A-Z]\d[A-Z] \d[A-Z]\d)$', 1, 1, NULL, 1) postcode
from tbl;

Related

How to extract specific string until blank space/next line from a text in Oracle?

I am trying to extract the following from the text field using Regrex in Oracle.
For example
"This is example,
and this really a example :h,j,j,j,j,
l //Updated question , as this letter is on the next line
now this is a disease:yes"
I am expecting a result as h,j,j,j,j,l, but if I use
REGEXP_SUBSTR(text_field,'example :[^:]+,') AS Result
I am getting example:h,j,j,j,j
But I am not getting the last letter 'l' like above and I am guessing that's because it's on the next line.Also, if I want the string "disease:yes" only, that will be so helpful as well. Thank you much!
The result you are getting is because your pattern includes the word 'example' and ends with a comma, leaving out the ending 'l'. Try this form instead. Note the example is shown using a Common table Expression (CTE). The WITH statement creates the table called tbl which just sets up test data, kind of like a temp table. This is also a great way to set up data when asking a question. This form of the REGEXP_SUBSTR() function uses a captured group, which is the set of characters after the string 'example:' until the end of that line in the multi-line field. From this you should be able to get the other string you are after. Give it a go.
WITH tbl(text_field) AS (
SELECT 'This is example,
and this really a example :h,j,j,j,j,l
now this is a disease:yes' FROM dual
)
SELECT REGEXP_SUBSTR(text_field,'example :(.*)', 1, 1, NULL, 1) AS Result
FROM tbl;
RESULT
-----------
h,j,j,j,j,l
1 row selected.
Edit based on new info. Since that last letter could be on it's own line, you'll need to allow for the newline. Use the 'n' flag to REGEXP_REPLACE() which allows the newline to match in the usage of the dot (match any character) symbol in regex. We switch to REGEXP_REPLACE as we'll need to return multiple capture groups. Here the WITH sets up 2 rows, one with an embedded newline in the data and one without. The capture groups are (going left to right) 1-the data after "example :" and ending in a comma, 2-the optional newline and 3-the next single character. Then replace the entire data with captured groups 1 and 3 (leaving out the newline).
NOTE this is very specific to the case of only 1 character on the following line.
WITH tbl(ID, text_field) AS (
SELECT 1, 'This is example,
and this really a example :h,j,j,j,j,
l
now this is a disease:yes' FROM dual UNION ALL
SELECT 2, 'This is example,
and this really a example :h,j,j,j,j,l
now this is a disease:yes' FROM dual
)
SELECT ID,
REGEXP_REPLACE(text_field, '.*example :(.*,)('||CHR(10)||')?(.).*', '\1\3', 1, 1, 'n') AS Result
FROM tbl;
ID RESULT
---------- ------------
1 h,j,j,j,j,l
2 h,j,j,j,j,l
2 rows selected.

How to convert a number with decimal values to a float in PL/SQL?

The issue is that I need to insert this number into json, and because the number contains a comma, json becomes invalid. A float would work because it contains a period not a comma.
I have tried using replace(v_decimalNumber,',','.') and it kind of works, except that the json property is converted to a string. I need it to remain some type of a numerical value.
How can this be achieved?
I am using Oracle 11g.
You just need to_number() function.
select to_number(replace('1,2', ',', '.')) float_nr from dual;
Result:
FLOAT_NR
1.2
Note that if your number has .0 like 1.0, the function will remove it and leave it only 1
The data type of v_decimalNumber is some type of character format as it can contain commas (,). Your contention is that it contains a number once the commas are removed. However there is NO SUCH THING until that contention has been validated since being character I can put any character(s) I want into it subject to any length restriction. As an example a spreadsheet column that should contain numeric data. However, it that doesn't apply users will often put N/A into telling themselves that it doesn't apply. Oracle will happily load this into your v_decimalNumber. (And that's 1 of many many ways non-numeric data can get into your column.) So before attempting to process as a numeric value you must validate it is in fact valid numeric data. The following demonstrates one such way.
with some_numbers (n) as
( select '123,4456,789.00' from dual union all
select '987654321.00' from dual union all
select '1928374655' from dual union all
select '1.2' from dual union all
select '.1' from dual union all
select '1..1' from dual union all
select 'N/A' from dual
)
, rx as (select '^[0-9]*\.?[0-9]*$' regexp from dual)
select n
, case when regexp_like(replace(n,',',null), regexp)
then to_number(replace(n,',',null))
else null
end Num_value
, case when regexp_like(replace(n,',',null), regexp)
then null
else 'Not valid number'
end msg
from some_numbers,rx ;
Take away: Never trust a character type column to contain specific data requirements except random characters. Always validate then put the data into the appropriately defined columns.

Prevent SQLite query from stripping leading zeros from numeric strings?

In my database, a table contains two columns each containing an 8 digit ASCII code, usually it's just alphanumeric. For example, a row might contain A123B45C in col1 and PQ2R4680 in col2.
I need to have a query/view that outputs a 4 character string calculated as the 2nd+3rd chars of these, concatenated. So in this example the extra column value would be 12Q2.
This is a cut-down version of the SQL I'd like to use, although it won't work as written because of zero stripping / conversion:
select
*,
(substr(col1, 2, 2) || substr(col2, 2, 2)) AS mode
from (nested SQL source query)
where (conditions)
This fails because if a row contains A00B23B4 in col1 and P32R4680 in col2, it will evaluate as 0032 and the query output will contain numeric 32 not 0032. (It's worse if col1 contains P1-2345 or "1.23456" or something like that)
Other questions on preventing zero stripping and string to integer conversion in Sqlite, all relate to data in tables where you can define a column text affinity, or static (quotable) data. In this case I can't do these things. I also can only create queries, not tables, so I can't write to a temp table.
What is the best way to ensure I get a 4 character output in all cases?
I believe you issue is not with substr stripping characters as this works as expected e.g. :-
Then running query SELECT substr(col1,2,2) || substr(col2,2,2) as mode FROM stripping
results in (as expected):-
Rather, your issue is likely how you subsequently utilise mode in which case you may need to use a CAST expression CAST expressions
For example the following does what is possibly happening :-
`SELECT substr(col1,2,2) || substr(col2,2,2) as mode, CAST(substr(col1,2,2) || substr(col2,2,2) AS INTEGER) AS oops FROM stripping`
resulting in :-

Query first letters of each words and full text search in one query

Suppose I have such data:
Lena
Lera
Elena
Mark
Allen
Paul
When user enters 'le' I need to return
Find words by first letters
After that should follow all another words that contains 'le' chars (full text search)
So it should return:
Lena
Lera
Alen
Elena
Something like that (this example does not return anything):
SELECT name FROM table WHERE name LIKE 'le%' AND like '%le%' ORDER BY name ASC
Thank you.
After it turns out that UNION mixes up the order of the sub selects, we'll try something else. You could try it with a custom ORDER BY. First order by whether it starts with your search term, then by name.
SELECT name
FROM table
WHERE UPPER(name) LIKE UPPER('%le%')
ORDER BY (CASE WHEN UPPER(name) LIKE UPPER('le%') THEN 1 ELSE 2 END),
name ASC
Comparing the strings with UPPER helps ignoring the case. But UPPER only support ASCII apparently, read this article for more information on the topic. It also contains other ways to ignore case when comparing strings.

Removing trailing Spaces from Long Datatype PL/SQL

I have a Long with a couple of sentences in it, at the end there is a huge amount of blank spaces that need removed.
The problem is that the I have wrote a function to convert this long to a Varchar2 and trim the spaces but this has not worked.
I have used, RTRIM, TRIM TRAILING, TRIM and even tried replace " " with "" (but that just removed all spaces even between words.
Example:
SELECT TRIM(comment)
FROM p_comments
WHERE p_domain = 'SIGNATURE'
AND p_code = c_p_code;
This did not work as it cannot perform the trim on a "LONG".
SELECT RTRIM(f_get_varchar(get_p_code('JOHN'))) FROM dual
Did not work and just returned the same result.
Does anyone have any ideas?
Managed to find the answer. I used a regular expression.
SELECT regexp_substr(cis.acs_reports.f_get_varchar(:p_pfo_code), '.+[^space::]') pfo_comment
FROM dual

Resources