I am newbie to the SQL.
I have a requirement where users might select any values from any of the 3 'multiple list' dropdown box. There might be cases where user can completely ignore any of the dropdown box out of the 3.
So i need a query which can handle that.
The equivalent select query with values looks like this.
SELECT * FROM TABLE WHERE
description1 LIKE '%SHK ABS%' OR
description2 LIKE '%SHK ABS%' OR
description3 LIKE '%SHK ABS%' OR
description4 LIKE '%SHK ABS%' and
**Year** in ('2017','2016') and
**Program** in ('CDPGM');
Problem:
I tried a lot but not able to write a query which accepts empty values in 'IN' clause.
Example: If user dint select any year and program, then empty values will be passed. Basically the query should act like it will ignore that columns in where clause.
Any idea how to achieve this ?
From what you posted I'm guessing you have a problem defining the blank value.
SELECT * FROM table
WHERE col_name IS NULL OR
col_name IN (<list_of_your_values>, '', 0,)
As per I understand there can be three type of empty values according to your case: NULL, Blank and Space(s).
The in clause to match those will look like:
SELECT * FROM table_name WHRE col_name in (null, '', <your other values>);
I have tested it in MySQL 5.6(latest version). For other versions/DBMSs you may need to use trim function:
SELECT * FROM table_name WHRE trim(col_name) in (null, '', <your other values>);
Edit:
For integer type column the empty valuea will be NULL only, OR you can also treat 0 as empty value:
SELECT * FROM table_name WHRE col_name in (null, 0, <your other values>);
Related
I was looking at this example. This makes it look like single quotes are not required in the where clause when you give a list of numbers. I am using an oracle database if that makes a difference.
https://www.sqlservertutorial.net/sql-server-basics/sql-server-in/
SELECT
product_name,
list_price
FROM
production.products
WHERE
list_price IN (89.99, 109.99, 159.99)
ORDER BY
list_price;
So why is toad complaining when I do this? It says invalid identifier as the error message.
This is the query that fails.
select * from table1 where id in (1, 2, 3, 4);
Toad does not complain when I do this.
select * from table1 where id in ('1', '2', '3', '4');
Check to make sure you are using your expected datatype. In my case I was using CHAR(12) thats why I needed quotes.
SELECT
COLUMN_NAME,
DATA_TYPE,
DATA_LENGTH,
DATA_PRECISION,
DATA_SCALE
FROM ALL_TAB_COLS
Where TABLE_NAME = 'table1';
If that doesn't work your table may be in capital letters so try this.
SELECT
COLUMN_NAME,
DATA_TYPE,
DATA_LENGTH,
DATA_PRECISION,
DATA_SCALE
FROM ALL_TAB_COLS
Where TABLE_NAME = upper('table1');
Since the question was about fetching different numbers, let's have a look on an easy way to do this.
Of course, if the column always holds numbers only and strings should not be allowed, we should just change its datatype.
And of course, if we just want to check for a few numbers like 1,2 and 3, we can use an IN clause like shown in the question.
But let's assume we can't change the datatype because also strings can occur and we need to accept a big range of numbers.
For example, we want to accept all numbers between 200 and 300. We dislike to write an IN clause with 101 values, don't we?
So we can use VALIDATE_CONVERSION to make sure we consider numeric entries only.
Then we use CAST AS INT to check the number should be between 200 and 300.
The query will be this one:
SELECT id
FROM table1
WHERE VALIDATE_CONVERSION(id AS NUMBER) = 1
AND CAST (id AS INT) BETWEEN 200 AND 300
ORDER BY id; -- sorting is of course not needed. Remove it if not intended.
We can try this out here: db<>fiddle
The results of my SQL query include null values. How do I filter out null values?
The syntax may vary depending on the database you are using but you can explicitly exclude nulls in the where clause. For example, the following will exclude null values in the primary_author field:
SELECT
date,
primary_author,
ISBN
FROM
books
WHERE
primary_author IS NOT NULL;
My example works on every database I know, so it should work for you =)
SELECT *
FROM TABLE_NAME
WHERE COLUMN_NAME IS NOT NULL
Here you can find a simple explanation and some examples: https://www.w3schools.com/sql/sql_null_values.asp
But some times you want to replace null values for a default value, like 'X', in this case, we should know the database for correct syntax, here some examples:
Oracle:
SELECT nvl(column_name,'X')
FROM TABLE_NAME
Sqlite:
SELECT ifnull(column_name,'X')
FROM TABLE_NAME
SqlServer:
SELECT coalesce(column_name,'X')
FROM TABLE_NAME
How to make MS Query work with comma separated parameters in Excel cell?
My query is:
SELECT *
FROM ABC
WHERE Id in (?)
When I put id number for example "1" the query works, but I want to put into a parameters cell a few id's 1, 2, 3, 4 etc, but then I'm trying to to this the query doesn't work... How can I put parameter with comma separated values?
there is 2 diff way to do it:
select * from abc where id in ('1','2','3') etc but not in excel - maybe use notepad++
second way :)
select * from abc where (id like '1' or id like '2' or id like '3') etc
:)
You can use IN in you sql query.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (1,2,3,4);
also try to use BETWEEN with comma as parameters.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
I have the following simple (i think) requirement:
Have one value lets say '1', then I have a table with one column which has more than one row.
What is the SQL required so that I return two columns, the first column having all values of '1', the second column being the values from the table.
Thanks,
Bruce
select 1 as yourValue, yourField from yourTable
that ?
edit:
Even more, if it's a value coming as parameter (in a variable) you could do the same
select #yourVariable as yourValue, yourField from yourTable
You just need to SELECT the value as a column like so:
SELECT '1', col1, ... FROM tbl
How do you write a SELECT statement that only returns rows where the value for a certain column is null?
Do you mean something like:
SELECT COLUMN1, COLUMN2 FROM MY_TABLE WHERE COLUMN1 = 'Value' OR COLUMN1 IS NULL
?
I'm not sure if this answers your question, but using the IS NULL construct, you can test whether any given scalar expression is NULL:
SELECT * FROM customers WHERE first_name IS NULL
On MS SQL Server, the ISNULL() function returns the first argument if it's not NULL, otherwise it returns the second. You can effectively use this to make sure a query always yields a value instead of NULL, e.g.:
SELECT ISNULL(column1, 'No value found') FROM mytable WHERE column2 = 23
Other DBMSes have similar functionality available.
If you want to know whether a column can be null (i.e., is defined to be nullable), without querying for actual data, you should look into information_schema.
Use Is Null
select * from tblName where clmnName is null
You want to know if the column is null
select * from foo where bar is null
If you want to check for some value not equal to something and the column also contains null values you will not get the columns with null in it
does not work:
select * from foo where bar <> 'value'
does work:
select * from foo where bar <> 'value' or bar is null
in Oracle (don't know on other DBMS) some people use this
select * from foo where NVL(bar,'n/a') <> 'value'
if I read the answer from tdammers correctly then in MS SQL Server this is like that
select * from foo where ISNULL(bar,'n/a') <> 'value'
in my opinion it is a bit of a hack and the moment 'value' becomes a variable the statement tends to become buggy if the variable contains 'n/a'.
select Column from Table where Column is null;
select * from tableName where columnName is null
For some reasons IS NULL may not work with some column data type. I was in need to get all the employees that their English full name is missing, I've used:
SELECT emp_id, Full_Name_Ar, Full_Name_En
FROM employees
WHERE Full_Name_En = '' or Full_Name_En is null