How to use a select statment inside a case statement - sql

I will do a select statement inside my case statement like this:
CASE
WHEN d.dependent_speed_type = 4 THEN (SELECT column FROM tablename)
END
But this is not working.
Can I realize a select statement inside my case statement? If the value 4 is inside my column "d.dependent_speed_type" then it should be select a special value from another table. How is the right syntax?

You have to use something that will always return one value, like SELECT MAX(column) FROM tablename) for example.

Related

Can I know the issue of this SQL query

I have this SQL query:
but I'm getting an error:
If I remove the comma-separated value from the variable, it is working fine. As well as if I remove the NULL checking feature it is working fine. Can I know the issue of this
It's because a CASE WHEN can only return 1 value.
And a STRING_SPLIT returns a resultset.
I assume something like this is what you want.
SELECT *
FROM Facility f
WHERE (#Facility IS NULL OR f.facilityCode IN (SELECT value FROM string_split(#Facility,',')))
This will get all records if the variable is null.
The function dbo.split will split the string in more than one value. This will confuse your subquery and the error you are receiving will be thrown.
In case you need what goes before the comma consider using:
select top 1 value
from dbo.split(#Facility, ','))
You want to say if the variable is NULL so ignore the WHERE statement, if so your query would be:
SELECT *
FROM Facility f
WHERE #Facility IN (select value from dbo.split(#Facility, ',')) OR #Facility IS NULL

SQL select statement to change two other column values based on a column that contains null

I would like to use a SQL select statement that has the condition 'where column A is NULL change column B values to be equal to column C values'. How would I be able to incorporate this logic into a SELECT statement (Not an UPDATE statement as I cant change the tables on the server but want to query them from the server).
SELECT final.*
FROM final
The actual table is in the image below, here I want to change column Old to match column DirectUse if the Change column is null.
Try Case statement:
SELECT
Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
CASE: https://www.w3schools.com/sql/sql_case.asp
Can also be incorporated by UNION ALL:
SELECT Old
FROM final where Change is not null
UNION ALL
SELECT DirectUse
FROM final where Change is null
Use a CASE expression:
SELECT Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
I think you basically you want:
SELECT
ColumnA
, CASE WHEN ColumnA IS NULL THEN ColumnC ELSE ColumnB END AS ColumnB
, ColumnC
, <any other columns>
FROM Final

Displaying an alternative result when derrived table is empty

I have this sql code where I try to display an alternative value as a result whenever the table is empty or the the single column of the top row when it is not
select top 1 case when count(*)!=0 then derrivedTable.primarykey
else 0 end endCase
from
(
select top 1 m.primarykey
from mytable m
where 0=1
)derrivedTable
The problem is that when I run this, I get the error message "column 'derrivedTable.primarykey' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
But when I put 'derrivedTable.primarykey' in the group by clause, I just get an empty table.
Does anyone hve a solution?
thanks in advance
You can use aggregation:
select coalesce(max(m.primarykey), 0)
from mytable m;
An aggregation query with no group by always returns exactly one row. If the table is empty (or all rows are filtered out), then the aggregation functions -- except for COUNT() -- return NULL -- which can be transformed to a value using COALESCE().
Such a construct makes me worry. If you are using this to set the primary key on an insert, then you should learn about identity columns or sequences. The database will do the work for you.
Can you try this below script-
SELECT
CASE
WHEN COUNT(*) = 1 THEN derrivedTable.primarykey
ELSE 0
END endCase
FROM
(
SELECT TOP 1 m.primarykey
FROM mytable m
WHERE 0 = 1
) derrivedTable
derrivedTable.primarykey;

SQL Query with if statement

I have a table which has a `department` column (allows null) but when I select that table and the field is null I don't want it to show Null but "-".
I'm told to put the if statement inside the select statement but I can't figure it out. How can I do this?
You want to use the function coalesce():
select coalesce(department, '-')
from table t
This is an ANSI standard function available in most databases.
You can use two methods:
1. Using CASE:
SELECT CASE WHEN department IS NULL
THEN '-'
ELSE department
END AS department FROM TableName
CASE evaluates a list of conditions and returns one of multiple possible result expressions. Read more here.
2. Using COALESCE:
SELECT COALESCE (department,'-') FROM TableName
COALESCE returns first parameter which is not null. Read more here.
You need to use the 'CASE WHEN' statement in your select query. Like this:
SELECT CASE WHEN Department IS NULL THEN Department = '-'
END AS DEPARTMENT
FROM Table_Name
You can use following code:
select ISNULL(department, '-') AS DEPARTM
from dbo.tbl_Department

If statement inside an sql query

I have a function in PL/SQL which has two SQL statements in it, they are as follows :
select access_level into v_access from users where user_ID = APEX_UTIL.get_username( APEX_UTIL.get_current_user_id );
and
for row in ( select distinct sub_category_lu
, f_get_display('SUB_CATEGORY', sub_category_lu) display
from files
where active = 'Y'
and publish_date <= sysdate
and category_lu = p_category
and key in ( 0, snl_key )
order by substr(sub_category_lu,1,2)
) loop
Now based on the output from the first sql statement, I want to add another 'and' condition in the second sql statement which is in the for loop.
like if v_access is 'xyz' then I want to add a condition in the for loop sql statement as
AND sub_category_lu NOT LIKE '03%' AND sub_category_lu NOT LIKE '04%'
else dont add this condition and keep the loop as above. How can I add an if statement according to this in the loop.
Thanks in advance.
Simply adding only:
(...) AND (v_access != 'xyz'
OR (sub_category_lu NOT LIKE '03%' AND sub_category_lu NOT LIKE '04%'))