Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: remove first 4 characters from a string

Tags:

string

sql

oracle

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;
like image 546
Gamidron Avatar asked Dec 29 '13 18:12

Gamidron


People also ask

How to remove the last character from a string in Oracle?

Can Oracle Trim the Last Character? Yes, this is possible with the TRIM function, but it’s probably better to use the SUBSTR function. The TRIM function needs you to specify which character to remove, and it removes it from the end of the string. A SUBSTR function can be written to remove the last character from a string.

How to remove the first characters of a specific column in SQL?

Here we will see, how to remove the first characters of a specific column in a table in SQL. We can do this task using the String function. String functions are used to perform an operation on an input string and return an output string. There are various string functions like LEN (for SQL server), SUBSTR, LTRIM, TRIM, etc.

How to trim a string in Oracle?

Oracle TRIM () function removes spaces or specified characters from the begin, end or both ends of a string. The following illustrates the syntax of the Oracle TRIM () function: The first argument allows you to specify which side of the string to trim. LEADING removes any leading character that equals the specified trim_character.

How to remove leading and trailing characters from a string in SQL?

Summary: in this tutorial, we will introduce you to the SQL TRIM function that removes both leading and trailing characters from a string. The TRIM function allows you to trim leading and/or trailing characters from a string. The following shows the syntax of the TRIM function.


3 Answers

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.

like image 194
Guntram Blohm Avatar answered Sep 18 '22 13:09

Guntram Blohm


update table_name set col_name=substr(col_name,5);
like image 39
Sai Avatar answered Sep 21 '22 13:09

Sai


try regexp, like:

SELECT regexp_replace(t.iban_nummer,'(.{4})(.*)','\2\1') FROM t;
like image 37
ElvisZhu Avatar answered Sep 17 '22 13:09

ElvisZhu