Hidden character in SQL column value in oracle - sql

I have a value in column of a table and somehow there is some hidden character at the end of the string. I cannot see it or remove it. The string is placed below. The total characters that I can see in this string is 25, but I when check the length of the string it is showing as 26. I tried TRIM function but thinking it could be a space, but it is not. How to remove this kind of characters from string in oracle query. Actually, I am using regexp_replace to replace some part of this string, but because of this issue the regex not able to match the last number in the string to replace everything before it.
28/110/41492/171486/98122
Here is my regex function
regexp_replace(trim(ATTRIBUTE_VALUE), '(^|.*?/)' || '98122' || '(/|$)', 'replaced' || '\2', 1, 1)

Do this in two steps:
remove all non-printable characters
apply your replace pattern
This is:
regexp_replace
(
regexp_replace(attribute_value, '[^[:print:]]'), -- printable string
'(^|.*?/)' || '98122' || '(/|$)', -- search pattern
'replaced' || '\2', -- replace pattern
1, -- position
1 -- occurrence
)

Related

Extract Specific Set of data from a String in Oracle

I have the string '1_A_B_C_D_E_1_2_3_4_5' and I am trying to extract the data 'A_B_C_D_E'. I am trying to remove the _1_2_3_4_5 & the 1_ portion from the string. Which is essentially the numeric portion in the string. any special characters after the last alphabet must also be removed. In this example the _ after the character E must also not be present.
and the Query I am trying is as below
SELECT
REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[^0-9]+',1,1)
from dual
The Data I get from the above query is as below: -
_A_B_C_D_E_
I am trying to figure a way to remove the underscore towards the end. Any other way to approach this?
Assuming the "letters" come first and then the "digits", you could do something like this:
select regexp_substr('A_B_C_D_E_1_2_3_4_5','.*[A-Z]') from dual;
This will pull all the characters from the beginning of the string, up to the last upper-case letter in the string (.* is greedy, it will extend as far as possible while still allowing for one more upper-case letter to complete the match).
I have the string '1_A_B_C_D_E_1_2_3_4_5' and I am trying to extract the data 'A_B_C_D_E'
Use REGEXP_REPLACE:
SQL> SELECT trim(BOTH '_' FROM
2 (REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[0-9]+', ''))) str
3 FROM dual;
STR
---------
A_B_C_D_E
How it works:
REGEXP_REPLACE will replace all numeric occurrences '[0-9]+' from the string. Alternatively, you could also use POSIX character class '[^[:digit:]]+'
TRIM BOTH '_' will remove any leading and lagging _ from the string.
Also using REGEXP_SUBSTR:
SELECT trim(BOTH '_' FROM
(REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[^0-9]+'))) str
FROM dual;
STR
---------
A_B_C_D_E

How to remove non-numeric characters (except full stop "." ) from a string in amazon redshift

I have been trying to figure out how to remove multiple non-numeric characters except full stop ("."), or return only the numeric characters with full stop (".") from a string. I've tried:
SELECT regexp_replace('~�$$$1$$#1633,123.60&&!!__!', '[^0-9]+', '')
This query returns following result : 1163312360
But I want the result as 11633123.60
Please try this:
The below regex_replace expression will replace all character which are not ("^") in the (range of 0-9) & "."
SELECT regexp_replace('ABC$$$%%11633123.60','([^0-9.])','') FROM DUAL;
It returns the expected output "11633123.60"

Function in Oracle SQL , is there a different version of the TRIM function?

Here's my sql :
V_FORMAT_VAL := REPLACE( TRIM(IN_CATS_XY) , ' ' , '' );
But this is an different format , than what is shown here :
TRIM( [ LEADING | TRAILING | BOTH [ trim_character ] string1 )
I don't understand the code above. What is the point of doing a REPLACE if TRIM already takes care of spaces ?
TRIM only deals with spaces at the start and end of the string. In truth there is no point in doing the TRIM since REPLACE will replace all spaces throughout the string, the most efficient way would be:
V_FORMAT_VAL := REPLACE(IN_CATS_XY, ' ');
As Replace will remove the string by default if no replacement string is offered.
TRIM removes spaces from the beginning and end, then REPLACE removes any remaining spaces that were in the string.
Trim takes care of spaces at the start and end of the string, or both. The replace is there to get rid of any spaces that appear inside of the string.
Basically, it would take a string like:
one two three
and turn it into
onetwothree
while also eliminating starting and end spacing.
trim() eliminates the spaces at the beginning and at the end of a string. replace() removes all spaces.
Actually I think trim() is useless in this snippet.
just try this:
select '>'||trim(' a nice string ')||'<' from dual ;
and this
select '>'||replace(' a nice string ', ' ')||'<' from dual ;
and see the differences.

Oracle: remove first 4 characters from a string

So I want to remove the first 4 characters from a string in oracle. Those characters can be different every time.
In my case I need to take away the first 4 characters of an IBAN and put them at the end of the string. I got the part of putting them to the end of the string but I can't get the first 4 characters to be removed. Every solution I find on the internet removes specified characters, not characters from a certain position in the string (1 to 4).
I used the code below to get the first 4 characters to the end of the string and wanted to try something similar for removing them at the front but without success.
SELECT SUBSTR(iban_nummer, 1, 4) INTO iban_substring FROM dual;
iban_nummer := iban_nummer || iban_substring;
See the docs:
substring_length ...
When you do not specify a value for this argument, then the function returns all characters to the end of string. When you specify
a value that is less than 1, the function returns NA.
So iban_nummer := substr(iban_nummer, 5) || substr(iban_nummer, 1,4) should work. The first part selects all characters beginning from the 5th, the second character numbers 1..4.
update table_name set col_name=substr(col_name,5);
try regexp, like:
SELECT regexp_replace(t.iban_nummer,'(.{4})(.*)','\2\1') FROM t;
Alternative way using regexp :
SELECT regexp_replace(t.iban_nummer,'^.{4}(.*)','\2\1') FROM dual;

SQL substr query

I need to understand this query as best as possible, thanks
substr(b_Aplicacion,1,4)
|| '-'
|| substr(b_Aplicacion,5,2)
|| '-'
|| substr(b_Aplicacion,7,2)
I assume you're aware of how the substr() function works. (If not, here's an explanation.)
In PLSQL || is a string concatenation operator.
Example: 'left' || ' - ' || 'right' evaluates to 'left - right'
Your example looks like it's reformatting a string that probably is a date like 20120102 into 2012-01-02
This expression inserts dashes into the string after the 4-th and 6-th position, and throws away characters after the 8-th position. For example, abcdefghijkl becomes abcd-ef-gh.
Substr cuts out three parts from the string: abcd, ef, and gh in my example. || '-' || glues the parts back together, inserting dashes in between. || between two string expressions represent concatenation, i.e. it makes one string by gluing the part on its left to the part on its right.
substr( string, start_position, [ length ] ) is performed like this:
string is the source string.
start_position is the position for extraction. The first position in the string is always 1.
length is optional. It is the number of characters to extract. If this parameter is omitted, substr will return the entire string.
The || represents concatenation.
So that query is separating the placcing a '-' character after 4th and 6th positions.
For example, if you have 20121221 as b_Aplicacion that query will return 2011-12-21.