REGEXP_CONTAINS not recognized - google-bigquery

Happy new years, stackoverflow!
I am trying to use some regex functions in bigquery but some of them return error as if I have the name wrong.
SELECT REGEXP_CONTAINS(path, r'^abc$') FROM [tablename]
Query Failed
Error: 2.24 - 2.26: Unrecognized function regexp_contains
Where as if I do a similar regex function, the function text in the editor changes color and the query works.
SELECT REGEXP_EXTRACT(path, r'^abc$') FROM [tablename]
It should work since it's documented in this link.
Does anyone know how to fix this?

BigQuery Legacy SQL and Standard SQL support different set of regular expression functions
Legacy SQL Regular Expression Functions:
REGEXP_MATCH, REGEXP_EXTRACT and REGEXP_REPLACE
Standard SQL Regular Expression Functions:
REGEXP_CONTAINS, REGEXP_EXTRACT, REGEXP_EXTRACT_ALL and REGEXP_REPLACE
So, in your case just make sure you use proper BigQuery SQL dialect
#standardSQL
SELECT REGEXP_CONTAINS(path, r'^abc$') FROM [tablename]

Related

starts_with in presto?

I am new to writing sql queries in presto and was looking for a function similar to 'starts_with'.
If a string starts with a given substring then the query needs to return that record.
In Postgresql, I am currently doing select * from tableA where name~'^Joh'. Whats the equivalent of this in Presto?
PostgreSQL and presto are RDBMS based on SQL. It is weird to see that you've learned a PostgreSQL proprietary add on (regular expressions) to the language before learning the standard SQL functions. In SQL you use LIKE for pattern matches:
select * from tableA where name like 'Joh%';
You can use Like in SQL. You can go through this link https://www.w3schools.com/sql/sql_like.asp. Using like you can search for a specified pattern.
In presto you can use regexp_like() which runs little faster than other like operators.For your case try below query which should provide you with expected functionality.
select regexp_like('John', '^John')

BigQuery standard sql - not match regex

I'm trying to create a query that will select everything
that is not matching a given regex
In legacy, we had REGEX_MATCH so I was able to do
WHERE x NOT REGEX_MATCH("[a-z]")
What would be the equivalent on standard SQL?
In BigQuery Standrad SQL you should use REGEXP_CONTAINS(value, regex) instead
For example
WHERE NOT REGEXP_CONTAINS(x, r'[a-z]')

Replacing a string without using regular expression in netezza

Is there any way to replace a string in netezza sql without using regular expression function (i.e. regexp_replace())
eg:
replace('perfect','fect','fume')
TIA
Without the SQL Extensions Toolkit you can just use substr and instr functions. You may have to run them multiple times depending on the recurrence of the string in question. Below is an example:
select substr(a.txt,1,instr(a.txt,'fect')-1)
||'fume'
||substr(a.txt,instr(a.txt,'fect')+length('fect'),255)
from (select 'perfect' as txt) a
In case you have the SQL Extensions Toolkit installed then you can use:
select sql_functions..replace('prefect', 'fect', 'fume')

SQL query giving an incorrect syntax error

Select COUNT(*) as 'Number'
From image
WHERE (image.current_phase = 'aggregation' AND (image.raw_filename REGEXP '%gordonpho%back%$'))
The above SQL query is giving me an incorrect syntax error. I want to get the number of rows from the table image where the column image.current_phase has aggregation as text. Also the column image.raw_filename ends with '%gordonpho%back%'.
Can anybody see the syntax error in my statement?
REGEXP isn't valid in T-SQL, but you don't need it anyway since your "regular expression" would be the same using LIKE anyway:
Select COUNT(*) as 'Number'
From image
WHERE (image.current_phase = 'aggregation'
AND (image.raw_filename LIKE '%gordonpho%back%')
SQL Server doesn't support regular expressions natively. Try using LIKE:
Select COUNT(*) as 'Number'
From image
WHERE (image.current_phase = 'aggregation'
AND (image.raw_filename LIKE '%gordonpho%back%'))
For times where you really need/want to use regular expressions, you'd have to do it via SQLCLR support (.NET code).
It's possible in your SQL implementation that image is a reserved word. Try quoting it with backticks (MySQL), square brackets [] (MSSQL) or double quotes " (most others).
Also, the %s in '%gordonpho%back%$' aren't treated as wildcards in regular expressions. Try replacing the literal with:
'.*gordonpho.*back.*$'

SQL Statement using LIKE

I want to know in a column named NUMTSO if there exists data with this format "WO#############", so what I'm doing is this:
select *
from fx1rah00
where numtso like 'WO[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
but I get nothing. What am I doing wrong?
This works fine for me in SQL Server. If you are not using SQL Server you will likely need some different syntax though as the pattern syntax is not standard SQL.
;with fx1rah00 As
(
select 'WO1234567890123' as numtso
)
select *
from fx1rah00
where numtso like
'WO[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
MySQL allows you to use regular expressions with the REGEXP keyword instead of LIKE. I suggest the following code:
SELECT *
FROM `fx1rah00`
WHERE `numtso` REGEXP 'WO[0-9]{13}'
What dbms is this? Some databases don't let use use regex in like clause just wildcards. If its oracle you could checkout REGEXP_LIKE or REGEXP for mysql.
I would do something like:
where NUMTSO like 'WO%'
and REGEXP_LIKE(NUMTSO, 'WO[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
by using the like and the regex check you can still range scan on an index if there was one.
The SQL standard does not support REGEXP in LIKE. They have a much more primitive pattern language. You'll need to add a function, or post-filter, or discover a DBMS-specific extension.