Make table or database to be case insensitive - sql

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'

Related

Read multiple columns from single table based on our input string

I want to select multiple columns from single table based on my given input string in sql select statement.
Example :
If input="table_medical" i want to select the columns like medi_col1,medi_col2,medi_col2
If input="table_pharmacy" i want to select the columns like medi_phar1,medi_phar2,medi_phar1
sql("select
case when $input="table_medical" then medi_col1) //like this
please help me to complete this.
If you want this in a single query, then the same number of columns is needed -- and have compatible types.
One method uses union all:
select medi_col1, medi_col2, medi_col2
from t
where #input = 'table_medical'
union all
select medi_phar1, medi_phar2, medi_phar1
from t
where #input = 'table_pharmacy';
SET #input="table_medical";
SELECT
CASE WHEN #input="table_medical" THEN medi_col1 ELSE medi_phar1 END as medi_col1,
CASE WHEN #input="table_medical" THEN medi_col2 ELSE medi_phar2 END as medi_col2
CASE WHEN #input="table_medical" THEN medi_col3 ELSE medi_phar3 END as col3
FROM MyTable
Data types for medi_col1 and medi_phar1 needs to be same (and for the rest of columns too)

Check if the first or second condition exists

I have a little problem selecting queries, namely when I want to check the first condition with the code below
select * from VL_Faktura_Queue where FAK_KundenNr=127849 AND (FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis ='0001-01-01')
, it shows me one position, but when I add a condition where I want to check if there is a FAK_KundenNr with FAK_BomatNr LIKE '% Verk%' OR FAK_BoMatNr Like 'Zus%' also throws me different values that do not fall under FAK_KundenNr = 127849, as I can easily check that it returns my values for this KundenNr, where there is 1 OR 2 condition.
this is my query:
select * from VL_Faktura_Queue where FAK_KundenNr=127849
AND (FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis ='0001-01-01') --this would be the first condition
or FAK_BoMatNr like 'Zus%' --and this the second condition
This is the individual selection I should get but in one query at the end
so my question is how can i get in one query select from these two query from the picture, thanks everyone for the help
Your parentheses are not sufficient. AND has precedence over OR, so you have FAK_KundenNr = 127849 AND (<first condition)> OR FAK_BoMatNr like 'Zus%'.
SELECT *
FROM VL_Faktura_Queue
WHERE FAK_KundenNr = 127849
AND
(
(FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis = '0001-01-01')
or
FAK_BoMatNr LIKE 'Zus%'
);
In your requirement, you need to combine the "AND" operator with other logical "OR" operator.
SELECT *
FROM VL_Faktura_Queue
WHERE
(
( FAK_BoMatNr LIKE '%verk%'
AND FAK_VerrechnetBis = '0001-01-01'
) -- 1st Condition
or
(FAK_BoMatNr LIKE 'Zus%') -- 2nd Condition
)
AND FAK_KundenNr = 127849;
Please check if this solution is working for you.

SELECT * FROM tablename WHERE 1

I've been curious. What are the differences between these respective queries:
SELECT * FROM `tablename`
SELECT * FROM `tablename` WHERE 1
SELECT * FROM `tablename` WHERE 1=1
2 and 3 are the same in MySQL, functionally 1 is also the same.
where 1 is not standard so, as others have pointed out, will not work in other dialects.
People add where 1 or where 1 = 1 so where conditions can be easily added or removed to/from a query by adding in/commenting out some "and ..." components.
i.e.
SELECT * FROM `tablename` WHERE 1=1
--AND Column1 = 'Value1'
AND Column2 = 'Value2'
As you know, all three produce the same results. (In a boolean context, MySQL treats the integer "1" as true -- in fact, any number that is not "0" is treated as true).
The MySQL optimizer is explicitly documented to remove constant conditions in the WHERE clause:
Constant condition removal . . .:
(B>=5 AND B=5) OR (B=6 AND 5=5) OR (B=7 AND 5=6)
-> B=5 OR B=6
Hence, all three will be compiled into exactly the same code.
They are all functionally equivalent and should have the same performance characteristics.
That said, the first and third are standard SQL. The second will cause some sort of boolean expression error in many databases. So, I would advise you to avoid that (I'm not sure whether it works or not in MySQL's strict SQL mode).
Often the third is used when constructing dynamic WHERE clauses. It makes it easy to add additional conditions as AND <condition> without worrying about lingering ANDs.
If you are asking about the differences in performances and results, there isn't any , 2 and 3 are the same WHERE TRUE , and they will result the same as the first one.
1 - SELECT * FROM table_name
Results in all the data from table_name (no filter)
2 - SELECT * FROM table_name WHERE 1
1 will be evaluated as TRUE , therefore - no filter - every record will be returned .
3 - SELECT * FROM table_name where 1=1
Same as the last one, 1=1 is a TRUE expression , therefore - no filter - every record will be selected.
All are the same but 2 and 3 are used to easily handle AND/OR conditions
like:
SELECT * FROM `tablename` WHERE 1=1 AND (columnname1 = 'Value' OR columnname2 = 'Value')
In 1, MySQL does not need to evaluate any WHERE conditions.
In 2 and 3, the where condition is static and not based on the rows' values. It will be evaluated with boolean logic and always be true.
Functionally, there is no difference. You should choose 1 for code clarity.
All are the same but 2 and 3 are used to create Dynamic queries for AND/OR conditions
sqlquery =" SELECT * FROM `tablename` where 1 =1 "
we use 2 and 3 format to make dynamic query so we already know "where" keyword is added and we keep adding more filters .
Like
sqlquery = sqlquery + "and columna =a"
"AND columna =a " then
after few lines if we have new filters we add "AND coulmnb =b " and so on
You don't have to check the sql query for where keyword as its placed in first or initial query
SELECT * FROM `tablename` WHERE 1=1 AND (columnname1 = 'Value' OR columnname2 = 'Value')
Otherwise we can write sqlquery = "SELECT * FROM tablename"
then
if there is no 'where' clause in sqlquery then
sqlquery = sqlquery + "where columna =a"
else
sqlquery = sqlquery + "and columna =a"
They all output the same answer. However the way 2 and 3 are written is mostly is in order to have control of the "Where" statement so it would make it easier to add it or remove it later.
I think that the first and third way are the proper way of writing it. If you need a where statement you do like in number 3 otherwise number 1 would be good enough.
In MS SQL 1 and 3 are same , however, option 2 will not work , option 2 is an invalid statement as in MS SQL, WHERE is used to compare some values. For Example:
Select * from 'myTable where ID = 3 (valid)
Select * from 'myTable where 1 = 1 is same as Select * from 'myTable where 2= 2 is same as Select * from 'myTable where 3= 3 you get the idea (valid) is same as Select * From 'myTable'
SELECT * FROM table_name : it will give you all the records of the
table with running any where statement.
SELECT * FROM table_name WHERE 1 : this where condition is always
true, its mostly used by hacker to get into any system. If you heard
about sql injections than 2 & 3 are scenarios which are forced to
build by hacker to get all the records of table.
SELECT * FROM table_name where 1=1 : This will give you all the
records of the table but it will compare the where statement and
then move forward, it's basically added to add or removed more
statements after that.
Result - Gives all the records in the table specified instead of tablename for all three queries
SELECT * FROM tablename WHERE 1 - Check this answer
SELECT * FROM tablename WHERE 1=1 - Check this answer
For more Info about WHERE clause optimizations check these : MYSQL, SQLite, SQL

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

SQL query with an OR clause and only one parameter

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% )