SQL query with an OR clause and only one parameter - sql

I was wondering if is there a way to do a query like this one (where Z is a variable)
SELECT * FROM table t WHERE t.X = Z OR t.Y = Z
writing only one time the Z. To be more specific I would like to do the same query like so
SELECT * FROM table t WHERE (t.X OR t.Y) = Z
I am using an Oracle DB and it (obviously) gives me an error when I try to execute it BUT I really like a way to do it like in the second query.
To make you know my situation X and Y are both VARCHAR2 and I already try something like
SELECT * FROM table t WHERE (t.X || t.Y) like '%Z%'
But, unluckily, it is not as accurate as the first one.

You can use in:
WHERE Z IN (t.X, t.Y)
Columns can be in the IN list as well as constants.

Try Like This
SELECT * FROM table t WHERE (t.X like %Z% OR t.Y like %Z% )

Related

Select * From Table Where Name in like (wildcards)

Is it possible to combine SQL statements such as
SELECT * FROM X WHERE Y LIKE '%a%' OR Y LIKE '%b%' OR Y LIKE '%c%' OR Y LIKE '%d%'
with something like
SELECT * FROM X WHERE Y IN ('a', 'b', 'c', 'd')
so I don't have to write one big statement such as it is now:
IF NOT EXISTS(SELECT * FROM X WHERE Y LIKE '%a%' OR Y LIKE '%b%' OR Y LIKE '%c%' OR Y LIKE '%d%')
BEGIN
/* code */
END
Would be very nice to use something like SELECT * FROM X WHERE Y IN LIKE ('%a%', '%b%', etc..)
Appreciate all help and suggestions, thanks.
Please use the below code for your problem ,try and let me know if still there is issue.
SELECT * FROM X WHERE Y LIKE '%[a-d]%'
If your database supports RegEx, you can use that. For example:
SELECT * FROM X WHERE Y SIMILAR TO '%(a|b|c|d)%' in PostgreSQL, and
SELECT * FROM X WHERE Y REGEXP 'a|b|c|d' in MySQL.
LIKE '%[a-d]%' won't work because LIKEs in most databases don't accept regex patterns. This will just expect the literal text [a-d] somewhere in the string. How each database interprets regular expression (or native pattern) varies a lot. So you need to tailor these things to your database. For example, the following will not work in PostgreSQL:
SELECT * FROM X WHERE Y SIMILAR TO 'a|b|c|d'
because PostgreSQL will expect the whole string to be 'a', 'b', 'c', or 'd'.

How can I select a row having a BigInt and a DateTime field starting with a specigic value?

I am working on a Microsoft SQL Server database and I have the following problem to implement these 2 simple select query.
So I have a table named MyTable that has a column OtherFieldFK of type BigInt with a value of 70016592107.
So for example I want search all the record in this table that have the OtherFieldFK starting with the value 70.
I tried to use like in this way:
select *
from MyTable
where OtherFieldFK like = '70%'
but it doesn't work. I think that like clause works only on string, is it right?
In this table I also have a DATETIME column named DataInizioGestione.
I tried to do the same thing:
select *
from DataInizioGestione
where OtherFieldFK like = '2016%'
but in this case it doesn't work either.
How can I correctly implement these 2 queries?
the first should be right, as you wrote:
select * from MyTable where OtherFieldFK like = '70%'
for the second should be converted to the date format in the nvarchar (es 121 with this format aaaa-mm-gg hh:mi:ss.mmm(24h)); in this way you can make the comparison with the like:
select * from MyTable where convert(nvarchar,DataInizioGestione,121) like = '2016%'
or you can directly compare the year:
select * from MyTable where year(DataInizioGestione) = 2016

Make table or database to be case insensitive

If I make a search for instance
"Candy" in where statement I retrieve 12 hits however if I write "candy" I retrieve 0 hits.
Is it possible to make the database or table to be case insensitive?
If yes, how?
Thank you
A simple solution might be to convert both the search string and the column you are searching on to lowercase using the LOWER() function in SQL Server. Something like:
SELECT whatever
FROM yourTable
WHERE LOWER(whatever) = LOWER(#searchString)
If you want to make your where clause case insensitive . convert left and right to the same case and then put condition .
If I take table as mytable with attributes X and Y . where filter is on Y then
select X , Y from mytable where UPPER(Y) = UPPER(#toSearch)
or
`select X , Y from mytable where LOWER(Y) = LOWER(#toSearch)`
In your case, maybe its easier to understand if i use your where clause ,example:
SELECT *
FROM yourTable
WHERE LOWER(whatever) = 'candy' or
SELECT *
FROM yourTable
WHERE UPPER(whatever) = 'CANDY'

like search on number column in SQL

How do I do a like search on a number column in SQL?
I want numbers which are like '0.0000%'.
I tried with
select * from emp where emp_id & '' like '123%'
select * from emp where CONVERT(varchar(20), emp_id) like '123%'
but in vain.
Please help me
Regardless of which DBMS you are using AND assuming you have a valid reason to do this, you have several ways to solve problems like these. I can think of three right now:
Convert the number to a string and use a LIKE operator on this:
select *
from emp
where to_char(emp_id) like '123%';
Use mathematical operators directly (like Andrey suggests), for example:
select *
from table
where num between 0 and 0.0001;
Construct a mathematical expression (actually, this is just another case of method 2), for example:
select *
from table
where abs(num - round(num, 5)) < 0.00001;
Use comparison operators (> and <):
select * from table where num > 0 and num < 0.00001
select 0.0001*1000 from dual if it is <1 means the 0.0001 has 3 or more zeros.
so i did like
select * from emp where emp_id*10000<1

SQL Query inside a function

I am using PostgreSQL with PostGis. I am executing a query like this:
select st_geomfromtext('point(22 232)',432)
It works fine. But now I want to take a value through a query. for example:
select st_geomfromtext('point((select x from data_name where id=1) 232)' , 432)
Here data_name is some table I am using and x stores some values. Now query inside is treated as a string and no value is returned.
Please help.
ERROR: syntax error at or near "select"
Try this:
select st_geomfromtext('point(' || x || ' 232)', 432) from data_name where id=1
Postgis has a function ST_MakePoint that is faster than ST_GeomFromText.
select ST_SetSRID(ST_MakePoint(x),432) from data_name where id=1;
While #muratgu answer is generally the way to go, one minor note:
A subquery gets you a different result when no row is found for id = 1. Then you get nothing back (no row), instead of:
select st_geomfromtext(NULL, 432)
If you need a drop-in replacement:
select st_geomfromtext('point('
|| (select x from data_name where id=1)
|| ' 232)' , 432)