I'm using a Concatenate Field and I'd like to use it on the WHERE of it
TABLE T_EXAMPLE
RED - FERRARI - F50
BLUE - PORSHE - S20
GREEN - CAMARO - T40
I have to show all this fields on a Grid, 'cause of this that I need to put all of them in one field, but I have to filter the Grid with One information that can be any one of the three, example:
SELECT
T.COLOR || T.CAR,T || T.Model AS FIELD1
FROM T_EXAMPLE T
WHERE FIELD1 LIKE '%FER%'
Here I want to filter the Ferrari, but I've tried to do like this but I got an error (Invalid Identifier).
Can anyone help me??
The alias FIELD1 can't be used in the WHERE clause, so just repeat the column concatenation in the WHERE clause. Like this:
SELECT
T.COLOR || T.CAR || T.Model AS FIELD1
FROM T_EXAMPLE T
WHERE T.COLOR || T.CAR || T.Model LIKE '%FER%'
Or, if you don't like the repetition, another option would be this:
SELECT FIELD1
FROM (
SELECT
T.COLOR || T.CAR || T.Model AS FIELD1
FROM T_EXAMPLE T
)
WHERE FIELD1 LIKE '%FER%'
Related
I have a table containing 2 columns: FIRST_PART, SECOND_PART. What I need is to run a query again another table using the FIRST_PART, SECOND_PART as LIKE.
So, something like: SELECT {fields} FROM {table} WHERE {column} LIKE {first_part}%{second_part}
I thought maybe some string I construct and use EXECUTE IMMEDIATE, but there must be another way......
You can use:
SELECT field1,
field2,
field3
FROM table_name t
WHERE EXISTS(
SELECT 1
FROM other_table o
WHERE t.column_name LIKE o.first_part || '%' || o.second_part
);
Table schools
id | address | name
1 | Rybničná 59, Bratislava | Stredná odborná škola elektrotechnická
2 | Ul. Sibírska 1, Trnava | Stredná odborná škola elektrotechnická
What I want
From client If I want to type:
Stredná odborná
stredná odborná
stredna odborna
It must find rows with id 1 and 2
If I want to type Bratislava or bratis It must find row with id 1
What I have
SELECT * FROM schools WHERE unaccent(address) LIKE ('%' || 'bratis' || '%');
I need to select from 2 columns (address and name)
To make the search case insentive, use ILIKE instead of LIKE. Then, you would want to remove the accents from the input string as well. At last, just use AND or OR to combine the two criteria (note that you could use the same search term for both columns - use OR in this case)
SELECT * FROM schools
WHERE unaccent(address) ILIKE ('%' || unaccent('bratis') || '%')
AND unaccent(name) ILIKE ('%' || unaccent('Stredná odborná') || '%')
I hope this works
SELECT * FROM schools
WHERE unaccent(address|| ' ' ||name) ILIKE ('%' || 'bratis' || '%');
I have requirement where I want show a column as Carryover/new.
The logic is as below.
if color exists in palette then mark the column 'Dev type' as new else carryover.
I have tried to put the business scenario using a hypothetical table structure and simillar query.
Pal table
ID PalName year
1 Pal 1 2017
2 Pal 2 2016
3 pal 3 2017
4 pal 4 2016
5 pal 5 2017
Color table
ID Color name requestedpalette
1 Red pal 1, pal 5,
2 Green na,
3 Black na,pal 1,pal 3
Season Table
ID Color ID Palette ID Season name
1 1 3 Summer
2 2 4 Winter
Query
WITH masterdata AS
(SELECT season name,
color name,
season.is AS SeasonID,
color.id AS ColorID
FROM season
INNER JOIN color ON color.id=season.colorid
INNER JOIN palette ON palette.id=season.paletteid
WHERE palette.year=2017 )
SELECT colorname,
CASE
WHEN EXISTS
( SELECT 1
FROM masterdata
WHERE ',' || color.requestedpalette LIKE '%,' || masterdata.PalName || ',%' ) THEN 'New'
ELSE 'Carryover'
END DevelopmentType
FROM color
Is there a better way to fetch the above without using WITH?
How to set a value of a column based on the entire result set in a SQL Query
Try:
SELECT color,
nvl((SELECT distinct 'New'
FROM pal
WHERE regexp_like(color.requestedpalette,
'^([^,]*, *)*' || PalName || '(,.*)*$')
AND year = 2017
), 'Carryover') DevelopmentType
FROM color
You don't have PalName in your with clause, so I don't think what you have would work as is. Since you are only comparing the requestedpalette to the PalName I didn't join all 3 tables by ids. If you need just palettes that have a season, then bring that table in to the subquery column.
Really, after cleaning up dataentry errors, you could just move the masterdata query into your main query as a subquery and left join it on
color.requestedpalette LIKE masterdata.PalName || ',%'
or color.requestedpalette LIKE '%, ' || masterdata.PalName || ',%'
or color.requestedpalette LIKE '%, ' || masterdata.PalName
or color.requestedpalette = masterdata.PalName
and it will work per your original design.
SELECT distinct colorname,
(case when PalName is not null
then 'New' else 'Carryover' end) DevelopmentType
FROM color
LEFT JOIN (
SELECT PalName
FROM season
INNER JOIN color ON color.id=season.colorid
INNER JOIN palette ON palette.id=season.paletteid
WHERE palette.year=2017
) masterdata
ON color.requestedpalette LIKE masterdata.PalName || ',%'
OR color.requestedpalette LIKE '%, ' || masterdata.PalName || ',%'
OR color.requestedpalette LIKE '%, ' || masterdata.PalName
OR color.requestedpalette = masterdata.PalName
Edited 16-Mar-2017 to account for PalName possibly being a substring of the PalName enclosed or not enclosed in commas in color.requestedpalette.
I have a table with CSV values as column. I want use that column in where clause to compare subset of CSV is present or not. For example Table has values like
1| 'A,B,C,D,E'
Query:
select id from tab where csv_column contains 'A,C';
This query should return 1.
How to achieve this in SQL?
You can handle this using LIKE, making sure to search for the three types of pattern for each letter/substring which you intend to match:
SELECT id
FROM yourTable
WHERE (csv_column LIKE 'A,%' OR csv_column LIKE '%,A,%' OR csv_column LIKE '%,A')
AND
(csv_column LIKE 'C,%' OR csv_column LIKE '%,C,%' OR csv_column LIKE '%,C')
Note that match for the substring A means that either A,, ,A, or ,A appears in the CSV column.
We could also write a structurally similar query using INSTR() in place of LIKE, which might even give a peformance boost over using wildcards.
there's probably something funky you can do with regular expressions but in simple terms... if A and C will always be in that order
csv_column LIKE '%A%C%'
otherwise
(csv_column LIKE '%A%' AND csv_column LIKE '%C%' )
If you don't want to edit your search string, this could be a way:
select *
from yourTable
where csv like '%' || replace('A,C', ',', '%') || '%'
For example:
with yourTable(id, csv) as (
select 1, 'A,B,C,D,E' from dual union all
select 2, 'A,C,D,E' from dual union all
select 3, 'B,C,D,E' from dual
)
select *
from yourTable
where csv like '%' || replace('A,C', ',', '%') || '%'
gives:
ID CSV
---------- ---------
1 A,B,C,D,E
2 A,C,D,E
Consider that this will only work if the characters in the search string have the same order of the CSV column; for example:
with yourTable(id, csv) as (
select 1, 'C,A,B' from dual
)
select *
from yourTable
here csv like '%' || replace('A,C', ',', '%') || '%'
will give no results
Why not store the values as separate columns, and then use simple predicate filtering?
I have a master table that contains a list of strings to search for. it returns TRUE/FALSE if any string in the cell contains text from the master lookup table. Currently I use excel's
=SUMPRODUCT(--ISNUMBER(SEARCH(masterTable,[#searchString])))>0
is there a way to do something like this in SQL? LEFT JOIN or OUTER APPLY would be simple solutions if the strings were equal; but they need be contains..
SELECT *
FROM t
WHERE col1 contains(lookupString,lookupColumn)
--that 2nd table could be maintained and referenced from multiple queries
hop
bell
PRS
2017
My desired results would be a column that shows TRUE/FALSE if the row contains any string from the lookup table
SEARCH_STRING Contained_in_lookup_column
hopping TRUE
root FALSE
Job2017 TRUE
PRS_tool TRUE
hand FALSE
Sorry i dont have access to the DB now to confirm the syntax, but should be something like this:
SELECT t.name,
case when (select count(1) from data_table where data_col like '%' || t.name || '%' > 0) then 'TRUE' else 'FALSE' end
FROM t;
or
SELECT t.name,
case when exists(select null from data_table where data_col like '%' || t.name || '%') then 'TRUE' else 'FALSE' end
FROM t;
Sérgio
You can use a combination of % wildcards with LIKE and EXISTS.
Example (using Oracle syntax) - we have a v_data table containing the data and a v_queries table containing the query terms:
with v_data (pk, value) as (
select 1, 'The quick brown fox jumps over the lazy dog' from dual union all
select 2, 'Yabba dabba doo' from dual union all
select 3, 'forty-two' from dual
),
v_queries (text) as (
select 'quick' from dual union all
select 'forty' from dual
)
select * from v_data d
where exists (
select null
from v_queries q
where d.value like '%' || q.text || '%');