SQL - Where clause with case statement and wildcards - sql

I'm working in Oracle's APEX environment, trying to return all values in one case and specific values in another case. If you aren't familiar with APEX, it uses low-code to produce a front-end page that is data driven; this case uses a dropdown to select from a list, and I'm comparing that list selection to values pulled from my database.
This creates the dropdown list (requires 2 columns - a display column [name], and a return column [id]):
select distinct 'All users' as name,
'99999' as id
from real_table
union
select distinct name,
id
from real_table
That input is stored in a variable we'll call :LIST_INPUT. I want to select all values from another_table when 'All users' is selected, and only those associated with the particular user when their name/id is selected. Below is the code I have to try and achieve that, but no dice.
select name,
id,
other_col1,
other_col2
from another_table
where case
when :LIST_INPUT like '99999' then '%'
else :LIST_INPUT
end like id
This works fine when a real user id is selected, but returns nothing when the 'All users' value is selected. My logic here is that I'm asking it to compare a wildcard to the ID field, so it should return everything, but instead it returns nothing.
Thanks!

Probably case statement is not necesasary, look:
select name,
id,
other_col1,
other_col2
from another_table
where id = :LIST_INPUT
OR :LIST_INPUT like '99999' -- You can use any other condition here

I think your LIKE operator has the operands in the wrong order. Shouldn't it be?
select name,
id,
other_col1,
other_col2
from another_table
where id like case
when :LIST_INPUT like '99999' then '%'
else :LIST_INPUT
end

Related

DB2 SQL Statement WHERE clause CASE WHEN in multiple conditions

This is what I am trying to do:
SELECT
id, name
FROM
users
WHERE
isActive=true
(AND CASE WHEN {param} != null THEN name={param} ELSE null END)
if the passed {param} is not null then only the AND operator will be added otherwise just isActive=true condition will be used.
You can use something like COALESCE or (in case of DB2) NVL.
SELECT
id, name
FROM
users
WHERE
isActive=true
AND name=COALESCE({param},name)
You didn't say how you pass parameters/variables, so i'll leave it to you. "{param}" could be replaced with a column or constant in this example
try this
SELECT
id, name
FROM
users
WHERE
isActive=true
AND (({param} is not null AND name={param}) OR ({param} is null))

How to replace a NULL value in SQL table?

I have a table dbo.People in a database and one of the columns is Name. Some of the fields however are missing a Name value. So I am trying to replace the empty field with No Name. There is also an ID column with values 1, 2, 3, 4, and so on.
What I have so far:
SELECT ISNULL(Name, 'No Name') FROM dbo.People;
I then want to group and count how many times that name occurs
SELECT COUNT(ID), Name
FROM dbo.People
GROUP BY Name;
This works, however my result still shows a blank value with how many times that blank value occurs, rather than No Name.
I realised thanks to Squirrel and other commenters that the way I was typing the query was making it two separate queries.
The correct way I found was to combine them into one:
SELECT COUNT(ID), ISNULL(Name, 'No Name')
FROM dbo.People
GROUP BY Name;
use case when
select sum(case when name='No Name' then 1 else 0 end) as cnt,name from
(
SELECT coalesce(Name, 'No Name' ) as name
FROM dbo.People
) t group by name
Do you only want to get the NULL occurences?
No need to overcomplicate things then, just remember that you cannot compare against a lack of value/data but you can check for it.
SELECT Count(ID)
FROM dbo.People
WHERE Name IS NULL

SQL Conditional selection - Can I use Case statement

I have a select query with multiple joins. Based on a value I need to set the value of one of the select values to 'N/A' if it's not it should be queried from the table. Can I use Case from this, pls let me know.
Assume the below query:
select distinct name,id,branch,Related
from table1;
And the requirement is
Need to set the "Related" to "N/A" if branch is not "computer science" else it should get the value from table1.
You may use case..when structure as :
select distinct name,id,branch,
( case when Related = 'computer science' then
related
else
'N/A'
end
) as related
from table1

Completing a given SQL statement so that another column is displayed in the end

I'm given the following statement:
SELECT id FROM record_database WHERE id = <up to me to complete the statement>
The record database has different fields, among which are id and name.
I'm supposed to complete this select statement so that it displays all the ids and all the corresponding names side by side, and this should be done using this one line of SQL code. A hint was given that UNION or OR can be used.
I tried variations of the following:
SELECT id FROM record_database WHERE id = '*'
UNION
SELECT name FROM record_database WHERE name = '*';
But none of these worked. I tried doing this with AND, tried using display columns, but those didn't work either.
Any help would be appreciated.
This smells a great deal like homework, so I won't offer a complete answer, but you can't just union queries that return dissimilar result sets. I'm inferring that ID is an integer while NAME is some varchar, which won't union as you've listed in your hint.
When you say "complete," are you restricted to adding things to the end? If so, its a non-starter. You can't increase the list of fields being returned merely by adding things to the "WHERE" clause. You need to add things to the actual field list to get them to be returned, so you might clarify whether you are truly restricted to appending to the query you;ve given.
If you are looking for:
id
name
id next
name next
Then use this trick:
SELECT col2
FROM (
SELECT id, col2=convert ( varchar (size of name field),id)
FROM table
WHERE ....
UNION ALL
SELECT id, name
FROM table
WHERE ....
)
ORDER BY id
This order by will bring id and name side by side and col2 will contain id in first row and name in second row.
Cheating. Make the select return 0 rows and add another one that will show 2 columns. All in one and the same line:
SELECT id FROM record_database WHERE id = NULL;SELECT id,name FROM record_database;
No more time should be wasted on silly problems like this.
If both id and name are char (or varchar), you could also do this, concatting the two columns into one:
SELECT id FROM record_database WHERE id = NULL
UNION ALL
SELECT id || '--' || name FROM record_database ;
The id || '--' || name part differs from one DBMS to another. In some, the + is the concat operator, in others there are special functions. So you may need to use:
id + '--' + name
or:
CONCAT(id, '--', name)
Try this
SELECT * FROM record_database WHERE id = '*' OR name = '*'

Inline table in Oracle SQL

I'm trying to integrate with some software (that I can't modify) that queries a database that I can modify.
I can give this software SQL queries, like so "select username, firstname, lastname from users where username in ?"
The software than fills in the ? with something like ('alice', 'bob'), and gets user information for them.
Thing is, there's another piece of software, which I again can't modify, which occasionally generates users like 'user2343290' and feeds them through to the first piece of software. Of course, it throws errors because it can't find that user.
So the query I want to run is something like this:
select username, firstname, lastname from users where username in ?
UNION ALL
select t.column1, 'Unknown', 'Unknown' from create_table(?) t
where create_table generates a table with the rows mentioned in ?, with the first column named column1.
Or alternatively:
select username, firstname, lastname from users where username in ?
UNION ALL
select t.column1, 'Unknown', 'Unknown' from _universe_ t where t.column1 in ?
where _universe_ is some fake table that contains possible every value in column1 (i.e. infinitely large).
I've tried select ? from dual, but unfortunately this only worked when ? was something like ('x'), not ('x', 'y').
Keep in mind I can't change the format of how the ? comes out, so I can't do select 'alice' from dual union all select 'bob' from dual.
Anyone know how I could do what I've mentioned, or something else to have a similar effect?
You can turn the delimited string of names into a table type like so:
CREATE TYPE name_tab AS TABLE OF VARCHAR2(30);
/
SELECT * FROM table(name_tab('alice','bob'));
So you would just need to create the type then your example would become:
select username, firstname, lastname from users where username in ?
UNION ALL
select t.column1, 'Unknown', 'Unknown' from table(name_tab ?) t
(I'm assuming that the ? is replaced by simple text substitution -- because the IN wouldn't work if it was done as a bind variable -- and that the substituted text includes the parentheses.)
However, I am not sure the result of this will be helpful, since when a list of good usernames is given, you'll now have two result rows for each username, one with the actual information and another with the 'Unknown' values.
A better way to phrase the query might be:
select t.column_value username,
NVL(users.firstname,'Unknown'),
NVL(users.lastname,'Unknown')
from table(name_tab ?) t left join users on users.username = t.column_value
That should give you one row per username, with the actual data if it exists, or the 'Unknown' values if it does not.
You could use a pipelined function:
create type empname_t is table of varchar2(100);
create or replace function to_list(p_Names in string) return empname_t pipelined is
begin
pipe row(p_Names);
return;
end;
select * from table(to_list('bob'))
If you need to split the names (e.g. 'bob,alice'), you could use a function accepting a string and returning a empname_t, e.g. Tom Kyte's in_list, see
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:210612357425
and modify the to_list function to iterate over the collection and pipe each item from the collection.