starts_with in presto? - sql

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')

Related

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]')

Use XML column in where clause in PostgreSQL

I want to supporting where clause for columns which containing XML content in hibernate for some DBs. I've achived this in Oracle by extending org.hibernate.dialect.OraclexDialect class and registering xml functions by registerFunction method. Example of the generated query:
SELECT *
FROM OM_PERSON this_
WHERE xmltype.createxml(this_.config_xml).existsNode('/*[condition/text()="19943"]')=1;
The function which is registered in hibernate (For Oracle):
xmltype.createxml(?1).existsNode(?2)
Now i want to support PostgreSQL (preferred version: 9.6) too, and i can't find any equivalent function for this. So my question is is there any equivalent function/statement to the above Oracle query in PostgreSQL?
PostgreSQL has a function XMLEXISTS, so your query can looks like:
SELECT *
FROM OM_PERSON x
WHERE XMLEXISTS('/*[condition/text()="19943"]' PASSING x.config_xml)
I have not idea, how it can be used in Hibernate.

REGEXP_CONTAINS not recognized

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]

Regular expressions in SQL?

I want to use a regular expression in Oracle 11g SQL to find records that do not match it. The regular expression is:
/([A-Z]{3})+([0-9])\w+/g
The SQL I want to use would be something like:
select
stu_code
,stu_insc
from
intuit.ins_stu
where
stu_insc not like ('/([A-Z]{3})+([0-9])\w+/g')
Obviously I know the above is not right, so does anyone know how I do this? I do not have the rights to run any PL/SQL.
On oracle you can try something along the lines of
select xyz
from theTable
where not regexp_like(mycolumn,pattern)

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.