This question already has an answer here:
how to replace non ascii characters with empty values
(1 answer)
Closed 2 months ago.
We have data in Postgres something as below , there is possibility of having mutiple non-ascii chars in a string
Name
Kate SolutionǸǸs
Etak Solutions
We are trying to identify if there are any NON-ASCII char set from the string and remove all of them if there are any(if there no non-ascii then keep the string as is) and expecting output below output
Name
Kate Solutions
Etak Solutions
Tried using below SQL but its just removing only one non-ascii char irrespective of the position where it is present
select REGEX_REPLACE(name,'[^ -~]', '') as new_name from table
Appreciate any help!
From the Postgresql documentation:
If the g flag is given, or if N is specified and is zero, then all matches at or after the start position are replaced. (The g flag is ignored when N is specified.)
So either add a 4th parameter as zero, 0, or add a 6th parameter as 'g'.
Related
This question already has answers here:
Match exact string
(3 answers)
Closed 2 years ago.
I want to validate one column AMOUNT Varchar(2) which should contains only integer value.
I am using below logic :
IF regexp_like(amount,'^[0-9]+') THEN
X_STATUS:=true;
else X_STATUS:=false;
X_REMARKS:='amount SHOULD BE INTEGER';
RETURN;
END IF;
But it doesn't work , Kindly help me for the optimal solution:
Thank You
This is your code:
regexp_like(amount,'^[0-9]+')
It checks if amount starts with a digit - but other characters may be anything. Typically, it would allow something like '1A', '2$' or 3%'.
Consider:
regexp_like(amount,'^[0-9]+$')
$ represents the end of the string in the regex, so this means: all characters must be digits.
If you want to allow the empty string as well (which the above expression does not do), you can change quantifier + to *.
You need an anchor for the end of the string:
IF regexp_like(amount, '^[0-9]+$')
This matches all characters in the string.
If you don't want leading zeros either, then:
IF regexp_like(amount, '^([1-9][0-9]*|0)$')
And this assumes that you don't want negative numbers. If you do:
IF regexp_like(amount, '^(-?[1-9][0-9]*|0)$')
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have to put a string after every 5 characters in a given string (varchar2).
Given string can have different length.
I already solved it by a loop using substrings.
Is there any way i could reach the goal using REGEXP in Oracle DB?
You can use REGEXP_REPLACE to replace every 5 characters with those 5 characters followed by another string. For example:
SELECT REGEXP_REPLACE('ABCDE12345FGHIJ67890KL', '(.{5})', '\1*') FROM DUAL
Output:
ABCDE*12345*FGHIJ*67890*KL
Demo on dbfiddle
I am trying to use regular expression in a sql statement to remove all spaces and digits from a string 'text' which may look like
1. 000000123456 (No space)
2. 00000 123456 (spaces in the beginning and end of string)
3. 90330000 45 (2 spaces at the end)
I have been able to come up with the solutions below so far:
select regexp_replace('text','\\s(^[0-9]*)\\s','\\1')
select regexp_replace('text','[[:blank:]]+[^[0-9]*][[:blank:]]+','\\1')
The results I get are:
1. 000000123456
2. 00000 123456
3. 90330000 45
I get the text as is. If I try to just remove the digits using
regexp_replace('text','^[0-9]*','\\1'),
it works fine- all digits get removed and the value results in ''(null). But the text with spaces does not remove the digits nor the space.
What am I doing wrong here?
Try using a character class which contains both digits and whitespace:
regexp_replace('text', '[0-9\\s]+', '')
This should remove all numbers and whitespace. But, it isn't completely clear what you are trying to do, because you did not show us the numbers in context.
This question already has answers here:
What does N' stands for in a SQL script ? (the one used before characters in insert script)
(7 answers)
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 5 years ago.
I found something weird in this query
SELECT *
FROM t1
WHERE t1.country = N'USA';
why N Letter is used ?
The N stands for "National Character" and it means that the content of the string is Unicode.
You should be using Unicode (nchar/nvarchar) whenever you might come across proper names or other entities that can contain characters outside of the default ASCII character set. If you don't surround such strings with the N prefix, you will lose data. For example:
SELECT N'ук ферт хер', 'ук ферт хер';
Results:
----------- -----------
ук ферт хер ?? ???? ???
You should also be sure to use the N prefix in your WHERE or other clauses against n(var)char columns. When you don't use the N prefix, you could suffer serious performance issues due to implicit conversion.
N is for the compatibility of a character. Used for representing unicode characters.
It declares the string as nvarchar data type, instead of varchar, and the difference is that varchar column only stores an 8-bit codepage and a nvarchar column can store any Unicode data
This question already has answers here:
SQL to find first non-numeric character in a string
(3 answers)
Closed 5 years ago.
I have a table called Person, and a NVarChar column called Notes.
The Notes column has a lot of text in it, but always begins with a number of some kind, with /'s inserted throughout.
For example:
1/23 some text
45/678/9%*&^%$##
02/468/ some other text
I need to select the first character position that isn't a digit or /.
I don't care whether the position is 0-based or 1-based; I can accommodate that after the fact.
In this example, if I'm using 1-based character positions, the selection should produce the following:
5
9
8
So you're looking for an index that matches some sort of pattern, say a pattern index. If we're whimsical, we might abbreviate it to PATINDEX.
SELECT PATINDEX('%[^0-9/]%', Notes)
FROM Person