sql select in string with a split function? - sql

how can i make a select from not normed table.
i have the table like this:
this table is not norme BNF 2.
i just want to select the NO_ID where DEPARTEMENT = something.
example:
when the input 44 then NO_ID =1
when the input 37 then NO_ID =3
when the input 13(not in table) then NO_ID = 5
of course when input = 44 it works:
SELECT [NO_ID]
FROM [T_TARIF_ZONE]
WHERE DEPARTEMENT = '44'
but how can i put in WHERE statement when the input = 37 or 13.
thanks you in advance,
Stev

try this:
SELECT [NO_ID]
FROM [T_TARIF_ZONE]
WHERE DEPARTEMENT like '%37%'

Please try:
select
NO_ID,
DEPARTMEMT
from
T
where
' '+DEPARTMEMT+' ' like
(case when #var=13 then ' FRANCE ' ELSE '% '+#var+' %' END)

Oracle version -
SELECT [NO_ID]
FROM [T_TARIF_ZONE]
WHERE INSTR(DEPARTEMENT, '44')>0 OR INSTR(DEPARTEMENT, '37')>0 OR INSTR(DEPARTEMENT, '13')>0
SQL Server version -
SELECT [NO_ID]
FROM [T_TARIF_ZONE]
WHERE PATINDEX('44', DEPARTEMENT)>0 OR PATINDEX('37', DEPARTEMENT)>0 OR PATINDEX('13', DEPARTEMENT)>0

Related

SQL: how to check if at least one keyword from a keyword list exists in a string

I have a table with the following schema,
id data_string
1 I have a pen.
2 Monday is not good.
3 I love Friday.
... ...
And I also have a list of keywords ['pen', 'love', 'apple'].
I want to write a SQL script to find if any of the keyword in my list of keywords can be found in the data_string column of the table.
And I'd like the output of the script be like
id keyword_exist
1 true
2 false
3 true
... ...
Is this doable with SQL?
Consider below
with keywords as (
select ['pen', 'love', 'apple'] list
)
select t.*, regexp_contains(data_string, r'' || pattern) keyword_exist
from your_table t,
( select array_to_string(list, '|') pattern
from keywords
)
if applied to sample data in your question - output is
Does this work?
SELECT ID, data_string,
CASE WHEN data_string LIKE '% pen %'
OR data_string LIKE '% love %'
OR data_string LIKE '% apple %' THEN 'True'
ELSE 'False'
END AS keyword_exist
FROM table_name

'<EOF>' in subquery source in Hive query

I am running a query on Hive similar to:
SELECT *
FROM (SELECT a
FROM b
WHERE
sex = 'M'
AND degree = 'Bs'
AND age = 15
AND name LIKE 'L%'
);
the error is:
cannot recognize input near '<EOF>' '<EOF>' '<EOF>' in subquery source
Adding a table alias for your subquery is necessary for Hive. Below I use 't1' as the alias:
SELECT *
FROM (SELECT a
FROM b
WHERE
sex = 'M'
AND degree = 'Bs'
AND age = 15
AND name LIKE 'L%'
) t1 ;
All the down-votes are unjustified. Hive often does not produce correct error and throws lazy "EOF" at you. In this case you just need to specify table alias for your sub-query. SELECT * FROM (.....) tbl_alias

Oracle Regex at least "1" match in a serie

I have a question related with oracle sql regex function.
I have a series of zeros and ones. It can vary as:
Ex:
1111000
000001
0101111
10000
If there is at least one "1" in this serie, then I want to output as "1"
otherwise I want to ouput "0". So I tried something like:
SELECT REGEXP_REPLACE('1,1,1,0','[0,]||[1]+','') FROM DUAL
However this just outputs "1's" out of series excluding "0"
So my question: How can I achieve this on oracle sql?
If I understand well and you only need to check whether a string contains '1' or not, you may not need regexp and INSTR could be enough:
select case
when instr(yourString, '1') = 0
then '1 is not in the string'
else '1 is in the string'
end
from dual
`LIKE %1%' will work for you as well...
with dt as (
select '00000000' seq from dual union all
select '00011000' seq from dual)
select
seq,
case when seq like '%1%' then 1 else 0 end as res
from dt;
SEQ RES
-------- ----------
00000000 0
00011000 1
Later on I also figured out that this also worked for me:
SELECT
REGEXP_REPLACE( 'MY_STRING' , '.?+1?[0,]', '') as COLUMN_NAME
FROM dual;

T-SQL Comma delimited value from resultset to in clause in Subquery

I have an issue where in my data I will have a record returned where a column value will look like
-- query
Select col1 from myTable where id = 23
-- result of col1
111, 104, 34, 45
I want to feed these values to an in clause. So far I have tried:
-- Query 2 -- try 1
Select * from mytableTwo
where myfield in (
SELECT col1
from myTable where id = 23)
-- Query 2 -- try 2
Select * from mytableTwo
where myfield in (
SELECT '''' +
Replace(col1, ',', ''',''') + ''''
from myTable where id = 23)
-- query 2 test -- This works and will return data, so I verify here that data exists
Select * from mytableTwo
where myfield in ('111', '104', '34', '45')
Why aren't query 2 try 1 or 2 working?
You don't want an in clause. You want to use like:
select *
from myTableTwo t2
where exists (select 1
from myTable t
where id = 23 and
', '+t.col1+', ' like '%, '+t2.myfield+', %'
);
This uses like for the comparison in the list. It uses a subquery for the value. You could also phrase this as a join by doing:
select t2.*
from myTableTwo t2 join
myTable t
on t.id = 23 and
', '+t.col1+', ' like '%, '+t2.myfield+', %';
However, this could multiply the number of rows in the output if there is more than one row with id = 23 in myTable.
If you observe closely, Query 2 -- try 1 & Query 2 -- try 2 are considered as single value.
like this :
WHERE myfield in ('111, 104, 34, 45')
which is not same as :
WHERE myfield in ('111', '104', '34', '45')
So, If you intend to filter myTable rows from MyTableTwo, you need to extract the values of fields column data to a table variable/table valued function and filter the data.
I have created a table valued function which takes comma seperated string and returns a table value.
you can refer here T-SQL : Comma separated values to table
Final code to filter the data :
DECLARE #filteredIds VARCHAR(100)
-- Get the filter data
SELECT #filteredIds = col1
FROM myTable WHERE id = 23
-- TODO : Get the script for [dbo].[GetDelimitedStringToTable]
-- from the given link and execute before this
SELECT *
FROM mytableTwo T
CROSS APPLY [dbo].[GetDelimitedStringToTable] ( #filteredIds, ',') F
WHERE T.myfield = F.Value
Please let me know If this helps you!
I suppose col is a character type, whose result would be like like '111, 104, 34, 45'. If this is your situation, it's not the best of the world (denormalized database), but you can still relate these tables by using character operators like LIKE or CHARINDEX. The only gotcha is to convert the numeric column to character -- the default conversion between character and numeric is numeric and it will cause a conversion error.
Since #Gordon, responded using LIKE, I present a solution using CHARINDEX:
SELECT *
FROM mytableTwo tb2
WHERE EXISTS (
SELECT 'x'
FROM myTable tb1
WHERE tb1.id = 23
AND CHARINDEX(CONVERT(VARCHAR(20), tb2.myfield), tb1.col1) > 0
)

Query table by string matching without using LIKE?

How can I query a table where first name starts with 'N' and last name starts with 'K' without using like?
Try something like the following:
SELECT * FROM mytable
WHERE SUBSTR(firstName, 1, 1) = 'N'
AND SUBSTR(lastName, 1, 1) = 'K';
you might try with > and < operators
e.g.:
WHERE NAME >= 'N' AND NAME < 'O'
but I don't guarantee you get each and every letter you would expect (especially with accentuated characters if any)
Scal
What about regular Expression ?
select * from table1 where regexp_like ( firstName, '^N*');
select * from table1 where regexp_like ( lastName, '^K*');