SQL Statement using LIKE - sql

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.

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

is there any way to use the SIMILAR TO operator in SQL Server or any other operators like Similar TO operator?

DELETE FROM TABLE_NAME WHERE COLUMN_NAME SIMILAR TO '%(_100|_200|_300)';
Above is a postgresql query.
Need an equivalent SQL Server query.
It looks like you want pattern matching against a regex. In SQL Server you could phrase this as:
DELETE FROM TABLE_NAME WHERE COLUMN_NAME LIKE '%(_[123]00)';
This searches for strings that end with '(_100)' or '(_200)' or '(_300)'.
In am unsure that you really want the parentheses: these are literals, meaning that it will actually search for parentheses in the string. If that's not what you want, then remove them.

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 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.*$'