how to use many LIKE conditions in SQL easily? [duplicate] - sql

This question already has answers here:
Combining "LIKE" and "IN" for SQL Server [duplicate]
(5 answers)
Closed 9 years ago.
I want to use many LIKE conditions in my query. I couldn't find a practical solution. I tried CONTAINS but it doesn't work.
Instead of using this
where EIO.DigiAddress like '%abc#abc.com%'
or EIO.DigiAddress like '%def#def.com%'
or EIO.DigiAddress like '%ghi#ghi.com%'
I want to use something like this:
CONTAINS(EIO.DigiAddress,'%abc#abc.com%', '%def#def.com%', '%ghi#ghi.com%')
OR
EIO.DigiAddress IN ('%abc#abc.com%', '%def#def.com%', '%ghi#ghi.com%')

Try this:
Create a temp table:
CREATE TEMPORARY TABLE temp (
alternate VARCHAR(20)
);
then:
INSERT INTO temp
VALUES ('%abc#abc.com%'), ('%def#def.com%'), ('%ghi#ghi.com%');
Select:
SELECT t.*
FROM tbl t JOIN temp p ON (t.col LIKE p.alternate);

I don't see anything wrong in using LIKE. But if you don't like LIKE then use this (for MS SQLSERVER)
where PATINDEX('%abc#abc.com%', EIO.DigiAddress) >0 OR
PATINDEX('%def#def.com%', EIO.DigiAddress) >0 OR
PATINDEX('%ghi#ghi.com%', EIO.DigiAddress) >0
Note: You have to use relevant string function for PATINDEX depending on the database you use.

You can do this with regular expressions:
where EIO.DigiAddress regexp '[a-c|e-g|g-i]{3}#{1}[a-c|e-g|g-i]{3}.com'

Related

Select into temp table in PostgreSQL? [duplicate]

This question already has answers here:
Creating temporary tables in SQL
(2 answers)
Closed 7 years ago.
How to create temporary table using select into in PostgreSQL. For example in SQL Select * into temp_tab from source_tab;
You can try to use Create Table As command like this:
CREATE TEMP TABLE mytable AS
SELECT * from source_tab;
From the docs:
This command is functionally similar to SELECT INTO, but it is preferred since it is less likely to be confused with other uses of
the SELECT INTO syntax. Furthermore, CREATE TABLE AS offers a superset
of the functionality offered by SELECT INTO.
The CREATE TABLE AS command allows the user to explicitly specify
whether OIDs should be included. If the presence of OIDs is not
explicitly specified, the default_with_oids configuration variable is
used.

Select statement don't work with double quotation in Oracle [duplicate]

This question already has answers here:
How to deal with SQL column names that look like SQL keywords?
(17 answers)
Closed 10 years ago.
My table has column called select (or NOT, or other reserved keys).
I tried select 'select' column but I couldn't because oracle throw exception like this:
invalid user.table.column or table.column or column specification
My select statement like this:
select f.select
from foo f.
I was searching internet and I found accepted answer: How do I escape a reserved word in Oracle?
then, I changed my query: Select "f"."select" from foo f, but throw exception too like this:
"c"."select": invalit identifier.
How to use "select" column in select (or update, insert) statement?
P.S: I'm using pl/sql developer tool for querying data.
Try,
Select f."select" from foo f;
When identifiers are quoted in Oracle, they become case sensitive.
So it's likely that you have to write the following:
select f."SELECT" from foo f
(or "Select", or however you've actually written the column name)
This should work fine:
Select f."select" from foo f

Possible to exclude or reorder a column from `*`? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
SQL exclude a column using SELECT * [except columnA] FROM tableA?
Is it possible to exclude a column from a select * from table statement with SQL Server?
I have a need for this and this is my only option other than parsing a raw SQL string to get out the required field names (I really don't want to do that).
Just to be bold. When the query is made I do not have access to the list of fields needed from the table but I do know which field I do not need. This is part of a complex multi-part query.
Surely there must be some way even if it's "hackish" such as using table variables or views
My other option is to reorder the columns. My problem is with ExecuteScalar SQL functions which get the first row and first column.
EDIT
I can't add an answer since this is now closed but the way I ended up doing it was like so:
;with results_cte as (
select (calculation) as calculated_column, * from table
)
select * into #temptable from results_cte
where calculated_column<10 /*or whatever*/
alter table #temptable
drop column calculated_column
select * from #temptable
drop table #temptable
Nope. You'll have to build your statement manually or just select *.
No.
Instead, you could check syscolumns to get all of the field names, or (perhaps) SELECT * and ignore that column.
If you use dynamic SQL, you can generate the query from metadata about the table or view (INFORMATION_SCHEMA.COLUMNS) and exclude columns that way. I do this a lot to generate triggers or views.
But there is nothing in the SQL language which supports this.
The best way to handle this would be to select * and then just not present the excluded column to your users in your frontend. As others have noted, SQL has no direct capability of doing an all-columns-except construct.

SQL IN Statement using like syntax? [duplicate]

This question already has answers here:
Is there a combination of "LIKE" and "IN" in SQL?
(28 answers)
Closed 9 years ago.
I would like to do something like this i.e., use wild card characters in the in clause:
SELECT * FROM mytable WHERE keywords IN ('%test%', '%testing%')
This is not supported in SQL Server.... Is there some other way to achieve it...
Looking for something other than:
SELECT * FROM mytable WHERE keywords like '%test%' or keywords like '%testing%' or.....
Nope, only other way I can think of is joining in to a temp table but then you have to eliminate duplicate rows.
This query of yours is horribly inefficient as it will unconditionally table scan. Perhaps you should look at separating the keywords or introducing a full text index.
If you would like to look into doing full text searches, then I would recommend looking into some of the heavy weights out there like sphinx or lucene when you evaluate the Sql Server full-text solution.
SELECT DISTINCT *
FROM
mytable M
JOIN
(
SELECT '%test%' AS pattern
UNION ALL SELECT '%testing%'
...
) foo ON M.keywords LIKE foo.Pattern
Could be a CTE or temp table too
This is not supported in MS SQL.... Is
there some other way to achieve it?
Yes, Full Text Search. At least for prefix wildcards.
You can use Regular Expression to do this.
In one Regular Expression you can define as many pattern as you want in one expression.
There are lot's of article about Regular Expression Matching in MS SQL SERVER.
I used RLIKE in MySQL to do Regular Expression Matching.
I also attached link1 & link2 for MS SQL SERVER Regular Expression Matching.

Oracle SQL: How to use more than 1000 items inside an IN clause [duplicate]

This question already has answers here:
SQL IN Clause 1000 item limit
(5 answers)
Closed 8 years ago.
I have an SQL statement where I would like to get data of 1200 ep_codes by making use of IN clause. When I include more than 1000 ep_codes inside IN clause, Oracle says I'm not allowed to do that. To overcome this, I tried to change the SQL code as follows:
SELECT period, ...
FROM my_view
WHERE period = '200912'
...
AND ep_codes IN (...1000 ep_codes...)
OR ep_codes IN (...200 ep_codes...)
The code was executed succesfully but the results are strange (calculation results are fetched for all periods, not just for 200912, which is not what I want). Is it appropriate to do that using OR between IN clauses or should I execute two separate codes as one with 1000 and the other with 200 ep_codes?
Pascal Martin's solution worked perfectly. Thanks all who contributed with valuable suggestions.
The recommended way to handle this in Oracle is to create a Temporary Table, write the values into this, and then join to this. Using dynamically created IN clauses means the query optimizer does a 'hard parse' of every query.
create global temporary table LOOKUP
(
ID NUMBER
) on commit delete rows;
-- Do a batch insert from your application to populate this table
insert into lookup(id) values (?)
-- join to it
select foo from bar where code in (select id from lookup)
Not sure that using so many values in a IN() is that good, actually -- especially for performances.
When you say "the results are strange", maybe this is because a problem with parenthesis ? What if you try this, instead of what you proposed :
SELECT ...
FROM ...
WHERE ...
AND (
ep_codes IN (...1000 ep_codes...)
OR ep_codes IN (...200 ep_codes...)
)
Does it make the results less strange ?
Actually you can use collections/multisets here. You'll need a number table type to store them.
CREATE TYPE NUMBER_TABLE AS TABLE OF NUMBER;
...
SELECT *
FROM my_view
WHERE period MEMBER OF NUMBER_TABLE(1,2,3...10000)
Read more about multisets here:
Seems like it would be a better idea, both for performance and maintainability, to put the codes in a separate table.
SELECT ...
FROM ...
WHERE ...
AND ep_code in (select code from ep_code_table)
could you insert the 1200 ep_code values into a temporary table and then INNER JOIN to that table to filter rows instead?
SELECT a.*
FROM mytable a
INNER JOIN tmp ON (tmp.ep_code = a.ep_code)
WHERE ...