select where substring - sql

SQL newbie here, but I can't find the solution to something that looks easy:
The following query does not seem to have a valid syntax (ORA-00904: invalid identifier), but its logic should be clear. How can I achieve this in a query that needs to be speedy?
SELECT * FROM table WHERE LEFT(column,4)="abcd"

For this purpose, you should use like rather than left(). First, Oracle doesn't support left() (you need substr() instead). Second, like can make use of indexes because the wildcard is not at the beginning of the string:
SELECT *
FROM table
WHERE column like 'abcd%';

Oracle and some other products have substr.
SELECT * FROM tablename WHERE substr(columnname, 1, 4) = 'abcd'
I.e. single quotes for string literals!
ANSI SQL has substring:
SELECT * FROM tablename WHERE substring(columnname from 1 for 4) = 'abcd'
And others have left:
SELECT * FROM tablename WHERE LEFT(columnname,4) = 'abcd'

Related

How to use regexp_like for wildcard search in oracle?

I am using the below query to get Ids like 12766% and 39998%. How I can use regexp_like to check 12%66 and 39%98?
select * from table t where regexp_like( UPPER(ID),'12766|39998')
You may use the following regex pattern:
^(12[0-9]*66|39[0-9]*98)$
Oracle query:
SELECT *
FROM yourTable
WHERE REGEXP_LIKE(ID, '^(12[0-9]*66|39[0-9]*98)$');
Demo
But actually, you might just want to use LIKE here:
SELECT *
FROM yourTable
WHERE ID LIKE '12%66' OR ID LIKE '39%98';
This would work find, so long as you don't mind exactly what comes in between the digits.
I found solution for this. We can use the below query for a%d to match strings like abcd,acd,aabcd etc. A period character (.) is a perfect replacement for % in regexp which can support one or more occurrence of any characters supported in database.
select * from table where REGEXP_LIKE (UPPER (ID), '^12.66.|EY39.98.')

SQL IN() multi-column condition with static right operand

I definitely know there is a right syntax for this but can't remember of find what it is: I need to do something like
SELECT
*
FROM
"table"
WHERE
("dep_date","ret_date") IN (('2019-10-10','2019-10-15'),('2019-10-11','2019-10-16'))
Except this syntax doesn't work. What is the correct syntax for defining a static multi-column lookup for the right operand of IN()?
You can try below way -
SELECT
*
FROM
"table"
WHERE ((dep_date='2019-10-10' and ret_date='2019-10-15') or
(dep_date='2019-10-11' and ret_date='2019-10-16'))
Use a subquery with UNION ALL after the IN clause:
SELECT
*
FROM
"table"
WHERE ("dep_date","ret_date") IN (
select '2019-10-10', '2019-10-15'
union all
select '2019-10-11','2019-10-16'
)
You can find more about Row Values in SQLite here: https://www.sqlite.org/rowvalue.html

select TableData where ColumnData start with list of strings

Following is the query to select column data from table, where column data starts with a OR b OR c. But the answer i am looking for is to Select data which starts with List of Strings.
SELECT * FROM Table WHERE Name LIKE '[abc]%'
But i want something like
SELECT * FROM Table WHERE Name LIKE '[ab,ac,ad,ae]%'
Can anybody suggest what is the best way of selecting column data which starts with list of String, I don't want to use OR operator, List of strings specifically.
The most general solution you would have to use is this:
SELECT *
FROM Table
WHERE Name LIKE 'ab%' OR Name LIKE 'ac%' OR Name LIKE 'ad%' OR Name LIKE 'ae%';
However, certain databases offer some regex support which you might be able to use. For example, in SQL Server you could write:
SELECT *
FROM Table
WHERE NAME LIKE 'a[bcde]%';
MySQL has a REGEXP operator which supports regex LIKE operations, and you could write:
SELECT *
FROM Table
WHERE NAME REGEXP '^a[bcde]';
Oracle and Postgres also have regex like support.
To add to Tim's answer, another approach could be to join your table with a sub-query of those values:
SELECT *
FROM mytable t
JOIN (SELECT 'ab' AS value
UNION ALL
SELECT 'ac'
UNION ALL
SELECT 'ad'
UNION ALL
SELECT 'ae') v ON t.vame LIKE v.value || '%'

DB2 sql query run

Trying to run some query for DB2, but get no results.
SELECT APPLICATION_ID,
CLIENT_WRKSTNNAME
FROM TABLE(MON_GET_CONNECTION(cast(NULL as bigint), -2)) AS t
WHERE APPLICATION_ID IN (SELECT ''''||APPLICATION_ID||''''
FROM SYSIBM.SYSDUMMY1)
The problem is in subquery in WHERE clause. If I'll run only the
SELECT ''''||APPLICATION_ID||''''
FROM SYSIBM.SYSDUMMY1
part and copy/paste the result into the big query - I would get needed result. Result of sub-query looks like this: '92.81.111.13.51632.13022516453', it must be String/varchar.
What am I doing wrong?
Your query does not make sense. Here is the version with table aliases:
SELECT t.APPLICATION_ID, t.CLIENT_WRKSTNNAME
FROM TABLE(MON_GET_CONNECTION(cast(NULL as bigint), -2)) AS t
WHERE t.APPLICATION_ID IN (SELECT ''''||t.APPLICATION_ID||'''' FROM SYSIBM.SYSDUMMY1);
One possibility is that the field APPLICATION_ID is being compared to itself, with quotes. SYSIBM.SYSDUMMY1 does not have a column called APPLICATION_ID. So, this query is equivalent to:
SELECT t.APPLICATION_ID, t.CLIENT_WRKSTNNAME
FROM TABLE(MON_GET_CONNECTION(cast(NULL as bigint), -2)) AS t
WHERE t.APPLICATION_ID = ''''||t.APPLICATION_ID||'''' ;
Another possibility is that APPLICATION_ID is a variable. In that case, you should name the variable something like v_APPLICATION_ID so it is not confused with the column name.
It looks like you want something like:
WHERE '.'||t.APPLICATION_ID||'.' like '%.'||v_APPLICATION_IDS||'.%' ;
This is assuming the list in v_APPLICATION_IDS is separated by periods. Usually, it would be separated by commas and you would use ',' instead of '.'.
WITH s AS (SELECT APPLICATION_ID AS application_id FROM SYSIBM.SYSDUMMY1)
SELECT CLIENT_WRKSTNNAME
FROM TABLE(MON_GET_CONNECTION(cast(NULL as bigint), -2)) AS t
INNER join s as s on s.application_id=t.APPLICATION_ID

Oracle select all rows using the contains keyword

I'm looking for a character that I can use in an Oracle contains to get ALL results.
If I search for the string "test" in a title column I use this statement:
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">test</textquery></query>') > 0
With this statement I get the rows which have the "test"-string included in title-column.
BUT: Is there any way to use contains and select ALL rows?
Would this work for you?
select * from my_table
where title like
'<query><textquery grammar="CTXCAT">%</textquery></query>'
This uses the LIKE syntax and the % wildcard to achieve what I think you want.
The documentation says wildcards with the Oracle Text CONTAINS() predicate are % and _ just like the LIKE predicate.
Does the following work? I don't have an instance of Oracle handy to test.
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">%</textquery></query>') > 0