How to trim leading zeroes in comma separated values in Oracle? - sql

I have a table CUSTOMER (ID,NAME,CORR_ID)
Sample Data -
ID|Name|Corr_ID
1 |John|0239,002319
2 |Mary|000466,00000000054667
Here is the output I need to see -
ID|Name|Corr_ID
1 |John|239,2319
2 |Mary|466,54667

regexp_replace(corr_id, '(,|^)0+', '\1')
will strip leading zeros without stripping zeros from 1000 or 2003.
The function looks for EITHER a comma OR the beginning of the string (^) - that is what (,|^) means - and then for one or more consecutive occurrences of the character '0'. It replaces each match with what was in the first pair of parentheses - that is, with the comma or the beginning of the string; it "zaps" all the leading zeros. You need to do it carefully, with the (,|^) - to make sure you don't drop zeros from 1000 or 2003.

Related

SQL, extract everything before 5th comma

For example, my column "tags" have
"movie/spiderman,genre/action,movie:marvel",
"movie/kingsman,genre/action",
"movie/spiderman,genre/action,movie:marvel,movie:dfjkl,movie:fskj,movie:aa,movie:mdkk"
I'm trying to return everything before 5th comma. below is the result example
"movie/spiderman,genre/action,movie:marvel",
"movie/kingsman,genre/action",
"movie/spiderman,genre/action,movie:marvel,movie:dfjkl,movie:fskj"
I've tried below code but it's not working.
select
NVL(SUBSTRING(tags, 1,REGEXP_INSTR(tags,',',1,5) -1),tags)
from myTable
You can use
REGEXP_REPLACE(tags, '^(([^,]*,){4}[^,]*).*', '\\1')
See the regex demo.
The REGEXP_REPLACE will find the occurrence of the following pattern:
^ - start of string
(([^,]*,){4}[^,]*) - Group 1 (\1 refers to this part of the match): four sequences of any zero or more chars other than a comma and a comma, and then zero or more chars other than a comma
.* - the rest of the string.
The \1 replacement restores Group 1 value in the resulting string.

SQL script to update all column values starting with number and - with blank in Postgresql

I need to update a varchar column's values
The values start with a number followed by - and then some letters
For Ex: 27-Check This
I need to update this value ie, I need to remove the starting number and the -
Expected output Example: Check This
NB: only the starting number and - should be removed all the values after the first alphabet should not be changed. Ie, if some number or - is present after the first alphabet then that should not be removed.
For ex: 27-Check 23-C This
Expected output: Check 23-C This
NB: I am new to sql so please help even if this looks simple to you
you can use regexp_replace to remove the leading digits:
update the_table
set the_column = regexp_replace(the_column, '^[0-9]{1,}\s*-\s*', '')
where the_column ~ '^[0-9]{1,}'
^[0-9]{1,}- in details:
^ match at the start of the string
[0-9]{1,} at least one number
\s* followed by zero or more (white) space
- followed by a dash
\s* followed by zero or more (white) space
The where clause ensure that only those columns are changed that need to be changed (e.g. values not starting with a number won't be touched at all).
If you just want everything after the first hyphen when the pattern starts with a number, you can use:
update t
set col = substring(col from '-(.*)')
where col ~ '^[0-9]+-';
substring() with a pattern is a nice implementation of what would be called regexp_substr() in other databases. It simply returns the first time the pattern is in the string. The full pattern is matched, but if there are parentheses, then only that portion is returned.

Trim Leading Zeroes Only If Numeric

I have a column containing a combination of numeric and alphanumeric values. When the value is strictly numeric, the database stores it with leading zeroes (but not always), but not if not.
Here's some sample data:
I need to use these values as part of a string that I will use to join to another table. Unfortunately, the portion of the string that corresponds to this field in the other table snips off the leading zeroes of any of the numeric-only values. I'm stumped finding a method of snipping the leading zeroes ONLY in this case.
I found this solution, but it's not for SQL Server (2012). Trim leading zeroes if it is numeric and not trim zeroes if it is alphanumeric
I also saw this, but it also removes the leading zeroes from the hyphenated values shown in the example, which doesn't work. Better techniques for trimming leading zeros in SQL Server?
Help! Thanks!
You could use:
select (case when col not like '%[^0-9]%'
then convert(varchar(255), try_convert(numeric(38), col))
else col
end)
This works for up to 38 digits after the leading zeros
The database does not store anything in varchar (text) fields except what you give it. If you give it leading zeroes, it will save them, it has no reason not to as it's just a piece of text.
For your problem, you can do this:
ISNULL(CAST(TRY_CAST(field AS numeric(38)) AS varchar(insert_field_length))), field)

Regular expression - capture number between underscores within a sequence between commas

I have a field in a database table in the format:
111_2222_33333,222_444_3,aaa_bbb_ccc
This is format is uniform to the entire field. Three underscore separated numeric values, a comma, three more underscore separated numeric values, another comma and then three underscore separated text values. No spaces in between
I want to extract the middle value from the second numeric sequence, in the example above I want to get 444
In a SQL query I inherited, the regex used is ^.,(\d+)_.$ but this doesn't seem to do anything.
I've tried to identify the first comma, first number after and the following underscore ,222_ to use as a starting point and from there get the next number without the _ after it
This (,\d*_)(\d+[^_]) selects ,222_444 and is the closest I've gotten
We can try using REGEXP_REPLACE with a capture group:
SELECT
REGEXP_REPLACE(
'111_2222_33333,222_444_3,aaa_bbb_ccc',
'^[^,]+,[^_]+_(.*?)_[^_]+,.*$',
'\1') AS num
FROM yourTable;
Here is a demo showing that the above regex' first capture group contains the quantity you want.
Demo

Insert comma after every 7th character using regex and hive sql

Insert comma after every 7th character and make sure the data is having comma after every 7th character correctly using regex in hive sql.
Also to ignore the space while selecting the 7th character.
Sample Input Data:
12F123f, 123asfH 0DB68ZZ, AG12453
112312f, 1212sfH 0DB68ZZ, AQ13463
Output:
12F123f,123asfH,0DB68ZZ,AG12453
112312f,1212sfH,0DB68ZZ,AQ13463
I tried the below code, but it didn't work and insert the commas correctly.
select regexp_replace('12345 12456,12345 123', '(/(.{5})/g,"$1$")','')
I think you can use
select regexp_replace('12345 12456,12345 123', '(?!^)[\\s,]+([^\\s,]+)', ',$1')
See the regex demo
Details
(?!^) - no match if at string start
[\s,]+ - 1 or more whitespaces or commas
([^\s,]+) - Capturing group 1: one or more chars other than whitespaces and commas.
The ,$1 replacement replaces the match with a comma and the value in Group 1.
You just want to replace the empty char to ,, am I right? the SQL as below:
select regexp_replace('12F123f,123asfH 0DB68ZZ,AG12453',' ',',') as result;
+----------------------------------+--+
| result |
+----------------------------------+--+
| 12F123f,123asfH,0DB68ZZ,AG12453 |
+----------------------------------+--+