Removing trailing Spaces from Long Datatype PL/SQL - plsql

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

Related

Extract Java comments from SQL statement in R

Trying to run a SQL statement in an RStudio environment, but I'm having difficulty extracting Java-style comments from the statement. I cannot edit the SQL statements / comments themselves, so trying a sequence of gsub to remove the unwanted special characters so I'm left with only the SQL statement in the R string.
I'm trying to use gsub to remove the special characters and the comment in between, but struggling to find the right regex to do so (especially one that does not read the division symbol in the SELECT statement as a part of the Java comment).
SELECT
id
, metric
, SUM(numerator)/SUM(denominator) AS rate
/*
This is an example of the comment.
I want to remove this. */
FROM table
WHERE id = 2
You can remove anything between /* and */ using this regex:
gsub(pattern = "/\\*[^*]*\\*/", replacement = "", x = text)
Result:
"SELECT\n id\n, metric\n, SUM(numerator)/SUM(denominator) AS rate\n/\nFROM table\nWHERE id = 2"

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

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;

Teradata remove enclosing single quotes from variable

I need to replace single quotes in a string of numbers and use in a WHERE IN clause. for example, I have
WHERE Group_ID IN (''4532','3422','1289'')
The criteria within parenthesis is being passed as a parameter, so I have no control over that. I tried using :
WHERE Group_ID IN (REGEXP_REPLACE(''4532','3422','1289'', '[']', ' ',1,0,i))
also tried using OReplace
WHERE Group_ID IN (OReplace(''4532','3422','1289'', '[']', ' '))
but get the same error:
[Teradata Database] [3707] Syntax error, expected something like ','
between a string or a Unicode character literal and the integer '4532'.
Please suggest how to remove the single enclosing quotes or even removing all single quotes should work as well.
The string ''4532','3422','1289'' you are using is incorrect because it contains non-escaped single quotes. This is a syntax error in SQL. In this particular form, no matter what function you use to fix it or which RDBMS you use, it will result in error with standard SQL.
Functions in the SQL cannot fix syntax errors. REGEXP_REPLACE and OReplace never get executed because the query never enters the execution state. It never goes past the SQL syntax parser.
To see the error from perspective of the SQL parser, you may break the string in to multiple parts
'' -- SQL Parser sees this as a starting and ending quote and hence an empty string
4532 -- Now comes what appears to SQL parser as an integer value
',' -- Now this is a pair of quotes containing a single comma
3422 -- Again an integer
',' -- Again a comma
1289 -- Again integer
'' -- Again emtpy string
This amalgam of strings and numbers will not mean anything to the SQL parser and will result in an error.
Fix
The fix is to properly escape the data. Single quotes must be escaped using another preceding single quote. So correct string in this scenario becomes '''4532'',''3422'',''1289'''
Another thing is that the OReplace usage (once syntax is fixed) is like OReplace(yourStringValueHere, '''', ' ')) Observe the usage of escaped single quote here. Two outer quotes are for the string start and end. First inner quote is the escape character and second inner quote is the actual data passed to the function.

SQLite: trim usage

My goal is to extract the domain out of given URL.
For that end I use the following:
select distinct ltrim(rtrim('https://www.youtube.com/watch?v=...', '/'), 'https://')
The result I get is:
www.youtube.com/watch?v=...
While the following is expected:
www.youtube.com
How can the above be achieved?
Note:
I notices that the trim function works differently than I expected.
select distinct ltrim('https://www.youtube.com/watch?v...', 'youtu') returns the same string without any change.
Trying to trim only the slash by select ltrim('https://www.youtube.com/watch?v...', '/') returns the same string as well.
Any explainations are welcomed.
Trim only removes the given characters at the beginning and/or end of the string.
You'll need substr and instr. (https://www.sqlite.org/lang_corefunc.html)
But the best option is probably to fix this in your code before saving it to the database.
At the end I didn't use trim but substr as offered.
The following worked:
select replace(substr(substr(<url>, instr(<url>, '//')+2),0,instr(substr(<url>, instr(<url>, '//')+2),'/')),'.','')
select replace(substr(substr(<url>, instr(<url>, '//www.')+6),0,instr(substr(<url>, instr(<url>, '//www.')+6),'/')),'.','')

Oracle REGEXP_LIKE Strange Behavior

Have a simple query it's failing for one set of parameters but it works for other parameters it's not working.
This works:
SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Encounters'
AND REGEXP_LIKE ('tplmcoce.41.20170822.txt', R.FILEMASK, 'i')
This is not working
SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Facets'
AND REGEXP_LIKE ('SMS-0162628062', R.FILEMASK, 'i')
If I have a column called Filemask (REGEX Pattern) in database so how can I select matching pattern for given string (file name)?
When I try the second query I am getting the following exception:
ORA-12725: unmatched parentheses in regular expression 12725. 00000 - "unmatched
parentheses in regular expression"
*Cause: The regular expression did not have balanced parentheses.
*Action: Ensure the parentheses are correctly balanced.
SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Facets'
AND REGEXP_LIKE ('^SMS[-]0162628062$*', R.FILEMASK, 'i') ---- Try and let us know output......................
Here is a thought...
select *
from <your_table>
where regexp_count( FILEMASK, '\(' ) != regexp_count( FILEMASK, '\)' )
;
You should find at least one where PRODUCER = 'Facets' (and perhaps more?)
... although this may not be enough. Consider this regexp: '\(abc)' (where you meant '\(abc\)'). You want literal parentheses around the string abc. But you only escaped the opening parenthesis and forgot to escape the closing one. For a regular expression, that is an error: the escaped parenthesis is treated as any other character, it is not "special" in any way; this leaves the closing parenthesis as a special character, and it is not matched by an opening one.
On the other hand, my solution above is not able to distinguish between escaped and non-escaped parentheses - it treats them all the same, so '\(abc)' looks perfectly fine to it. If what I posted above is not enough, you/we will need more subtle ideas. Perhaps:
...
where regexp_count( FILEMASK, '([^\]|^)\(' ) != regexp_count( FILEMASK, '([^\]|^)\)'
This only looks for non-escaped parentheses. [^\] means "any character other than a backslash", and ([^\]|^) means either that or the beginning of the string.
This issue is resolved. This is because I had one bad regular expression (missed one parenthesis) which caused the failure. The REGEX_LIKE started comparing each mask in table column when it come to the bad regex it failed.
Please close this question.
Thanks all.
Escape the Chars in the [] with '\' . And you have to replace the last char '.' with * in the second pattern('SMS...')
^tplmcoce[\.](37|41|47|57)[\.].*[\.]txt$
and
^SMS[\-]0162628062$*
SELECT 1 Matched
FROM DUAL
WHERE REGEXP_LIKE ('tplmcoce.41.20170822.txt',
'^tplmcoce[\.](37|41|47|57)[\.].*[\.]txt$', 'i');
SELECT 1 Matched
FROM DUAL
WHERE REGEXP_LIKE ('SMS-0162628062', '^SMS[\-]0162628062$*', 'i')

Resources