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)$')
Related
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'.
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
I have a questions regarding below data.
You clearly can see each EMP_IDENTIFIER has connected with EMP_ID.
So I need to pull only identifier which is 10 characters that will insert another column.
How would I do that?
I did some traditional way, using INSTR, SUBSTR.
I just want to know is there any other way to do it but not using INSTR, SUBSTR.
EMP_ID(VARCHAR2)EMP_IDENTIFIER(VARCHAR2)
62049 62049-2162400111
6394 6394-1368000222
64473 64473-1814702333
61598 61598-0876000444
57452 57452-0336503555
5842 5842-0000070666
75778 75778-0955501777
76021 76021-0546004888
76274 76274-0000454999
73910 73910-0574500122
I am using Oracle 11g.
If you want the second part of the identifier and it is always 10 characters:
select t.*, substr(emp_identifier, -10) as secondpart
from t;
Here is one way:
REGEXP_SUBSTR (EMP_IDENTIFIER, '-(.{10})',1,1,null,1)
That will give the 1st 10 character string that follows a dash ("-") in your string. Thanks to mathguy for the improvement.
Beyond that, you'll have to provide more details on the exact logic for picking out the identifier you want.
Since apparently this is for learning purposes... let's say the assignment was more complicated. Let's say you had a longer input string, and it had several groups separated by -, and the groups could include letters and digits. You know there are at least two groups that are "digits only" and you need to grab the second such "purely numeric" group. Then something like this will work (and there will not be an instr/substr solution):
select regexp_substr(input_str, '(-|^)(\d+)(-|$)', 1, 2, null, 2) from ....
This searches the input string for one or more digits ( \d means any digit, + means one or more occurrences) between a - or the beginning of the string (^ means beginning of the string; (a|b) means match a OR b) and a - or the end of the string ($ means end of the string). It starts searching at the first character (the second argument of the function is 1); it looks for the second occurrence (the argument 2); it doesn't do any special matching such as ignore case (the argument "null" to the function), and when the match is found, return the fragment of the match pattern included in the second set of parentheses (the last argument, 2, to the regexp function). The second fragment is the \d+ - the sequence of digits, without the leading and/or trailing dash -.
This solution will work in your example too, it's just overkill. It will find the right "digits-only" group in something like AS23302-ATX-20032-33900293-CWV20-3499-RA; it will return the second numeric group, 33900293.
CASE
WHEN <in_data> LIKE '[0-9][0-9][0-9][0-9][0-9][0-9]' THEN SUBSTR(<in_data>,1,3)
ELSE '000'
END
We're doing a migration project from Sybase to Teradata, and having a problem figuring this one out :) I'm still new to Teradata.
I would like to ask the equivalent TD code for this -
LIKE '[0-9][0-9][0-9][0-9][0-9][0-9]' to Teradata
Basically, it just checks whether the digits are numeric value.
Can someone give me a hint on this
You can also use REGEXP_SUBSTR to directly extract the three digits:
COALESCE(REGEXP_SUBSTR(in_data,'^[0-9]{3}(?=[0-9]{3}$)'), '000')
This looks for the first three digits and then does a lookahead for three following digits without adding them to the overall match.
^ indicates the begin of the string, '$' the end, so there are no other characters before or after the six digits. (?=...) is a so-called "lookahead", i.e. those three digits are checked, but ignored.
If there's no match the regex returns NULL which is changed to '000'.
You need to use regexp instead of like, since [0-9][0-9][0-9][0-9][0-9][0-9] is a regular expression.
To do an exact match, you need to add anchors. ie, to match the string which contains an exact 6 digit chars.
regexp '^[0-9]{6}$'
or
regexp '^[[:digit:]]{6}$'
This question already has an answer here:
Convert fraction to decimal [closed]
(1 answer)
Closed 9 years ago.
Working on 10g.
I tried to write a regexp that removes everything before the first dash(-).
However, I did not anticipate the complexity of the syntax.
I have this:
REGEXP_SUBSTR (myVal, '[^"]+')
This removes all the values after the first double quotation leaving me with data like:
10-3/4, 5-1/2, 7-5/8
I assumed that changing the double quotes to a dash and putting the carat after the dash would do it, but no luck.
If that's all you need to do, then you don't need to use REGEX, since you could just do it with other simpler functions:
SELECT SUBSTRING(myVal,INSTR(myVal,'-'),LENGTH(myVal))
FROM YourTable
If you want to do that with REGEXP_SUBSTR, try this:
SELECT REGEXP_SUBSTR(myValue, '\-.*$')
FROM myTable
But the SUBSTR answer from Lamak is just as effective. The third SUBSTR parameter defaults to the length of the string so you can leave it off:
SELECT SUBSTR(myValue, INSTR(myValue, '-'))
FROM myTable