I need to check whether a column in a table having a numeric value followed by decimal point and 3 precisions after the decimal point. Kindly suggest how to do using regular expression in postgre SQL (or) any other alternate method.
Thanks
The basic regex for digits, a period and digits is \d+\.\d{3}
You can use it for several things, for instance:
1. Add a Constraing to your Column Definition
ALTER TABLE mytable ADD (CONSTRAINT mycolumn_regexp CHECK (mycolumn ~ $$^\d+\.\d{3}\Z$$));
2. Find Rows that Don't Match
SELECT * FROM mytable WHERE mycolumn !~ $$^\d+\.\d{3}\Z$$;
3. Find Rows that Match
SELECT * FROM mytable WHERE mycolumn ~ $$^\d+\.\d{3}\Z$$;
Related
I have a requirement in which I need to extract the data based on a filter on a column and the filter would be to extract only alphanumeric values which means that it should contain at least one alphabet and a number for consideration.
For example if I have five numbers such as 333,abc,ab333,+33,+ab33 the output should have only ab333 and +ab33.
I was trying to implement this using the rlike function and the query is as below but this is giving all the records in the table.
select column_name from table_name where column_name rlike '^[a-zA-Z0-9]+$';
I also tried a different approach by using the below query but in case of special characters such as + the below query gives the wrong result.
select column_name from table_name where column_name not rlike '^[0-9]+$';
Could anybody guide me regarding the mistake of if there is a different approach for this.
You can use
RLIKE '^\\+?(?:[0-9]+[a-zA-Z]|[a-zA-Z]+[0-9])[0-9a-zA-Z]*$'
Details:
^ - start of string
\+? - an optional + symbol
(?:[0-9]+[a-zA-Z]|[a-zA-Z]+[0-9]) - one or more digits followed with a letter or one or more letters followed with a digit and then
[0-9a-zA-Z]* - zero or more alphanumeric chars
$ - end of string.
How do I add to the where clause of a sql select statement to match a particular regex.
I have a table with phone numbers. The phone numbers are 10 digits long. The data is dirty, so I want to not select records that are not in this format. like this:
select * from Phones where Phones like `RegExp("^\\d{9}$")`; <-- this doesn't work
Thanks
For BigQuery Standard SQL - use below (assuming your regexp itself is correct)
WHERE REGEXP_CONTAINS(Phones, r'^\d{10}$')
above will filter out any row where Phone is not 10 digits string
Please check the link for the result and table info. I need to query rows
with value '343' in Col B with a regular expression . All columns are strings . Also please be kind enough to point any good learning materials in how to write good REGEX in Hive . Thank you
For Hive use this:
select * from tablename where B rlike '343';
Checking it works:
hive> select '123435' rlike '343';
OK
_c0
true
Negative test:
hive> select '12345' rlike '343';
OK
_c0
false
Time taken: 1.675 seconds, Fetched: 1 row(s)
Hive uses Java flavor regex. You can find good reference and practice here: https://regexr.com/ and of course regex101
this will work:
select * from tablename where regexp_like(B,'(.*)(343)(.*)');
hive equivalent is :
select * from tablename where rlike(B,'(.*)(343)(.*)');
What I am looking to do is to, within Postgres, search a column for a string (an account number). I have a log table, which has a parameters column that takes in parameters from the application. It is a paragraph of text and one of the parameters stored in the column is the account number.
The position of the account number is not consistent in the text and some rows in this table have nothing in the column (since no parameters are passed on certain screens). The account number has the following format: L1234567899. So for the account number, the first character is a letter and then it is followed by ten digits.
I am looking for a way to extract the account number alone from this column so I can use it in a view for a report.
So far what I have tried is getting it into an array, but since the position changes, I cannot count on it being in the same place.
select foo from regexp_split_to_array(
(select param from log_table where id = 9088), E'\\s+') as foo
You can use regexp_match() to achieve that result.
(regexp_match(foo,'[A-Z][0-9]{10}'))[1]
DBFiddle
Use substring to pull out the match group.
select substring ('column text' from '[A-Z]\d{10}')
Reference: PostgreSQL regular expression capture group in select
I've tried searching online for the answer to this, but my Google-fu has failed me.
I have an Access database containing records represented by a string. The first 3 characters of that string are a 3-digit representation of the 366-day calendar date on which the record was created (000-366...yes, leap days count).
I'm having trouble coming up with the correct pattern match to include in a query that matches a 3-digit substring that can be between 000 and 366, where you don't lose the significant figures.
I know the query would be something like:
SELECT * FROM myTable WHERE Field1 LIKE "^[0-2]## or 3[0-5]# or 36[0-6]*";
...but I can't find any resource that says, in MS Access, what the "or" operator is. I tried "||" (double pipe) and "|" (single pipe), neither of which worked.
Is there an "or" operator that can be used with a MS Access pattern match?
The LIKE operator in Access is pretty limited, and doesn't support most of the features more 'fully-fledged' regular expression engines provide.
Instead, use multiple conditions in your WHERE clause like this:
SELECT *
FROM myTable
WHERE Field1 LIKE "[0-2]##*" OR
Field1 LIKE "3[0-5]#*" OR
Field1 LIKE "36[0-6]*"
Another alternative is to simply extract the first 3 characters to a string, convert them to an integer and test to see if their value is within the acceptable range.
Why not just pull the first three characters?
SELECT * FROM myTable WHERE CInt(Left(Field1,3)) <= 366
http://www.techonthenet.com/access/functions/datatype/cint.php