Selecting a specific value within a case statement - sql

I need help with selecting a specific value within a CASE Statement.
For an example
SELECT CASE WHEN Field1 >= (Field2 = 1) THEN Answer
WHEN Field1 < (Field2 = 1) THEN No Answer
ELSE Question END AS Field3
I want to be able to select a specific value in Field2 within the CASE Statement

could be you are looking for
SELECT CASE WHEN Field1 >= Field2 AND Field2 = 1 THEN Answer
WHEN Field1 < Field2 AND Field2 = 1 THEN No Answer
ELSE Question END AS Field3
or more simply
SELECT CASE WHEN Field1 >= 1 THEN Answer
WHEN Field1 < 1 THEN No Answer
ELSE Question END AS Field3

-- 1. You don't specify the fields types.
-- 2. You do a comparison of Field1 with a logical expression (Field2=1). i guess it fails.
-- 3. I will assume Answer, No answer and Question will be the desired string result, and have to be quoted.
-- 4. A Table name is missing. I will assume Field1 and Field2 are fields from MyTable
-- 5. I' am assuming Field1 and Field2 of the same type, and be varchar(10) for example.
DECLARE #Field2 varchar(10) = 1
SELECT
CASE WHEN Field1 >= Field2 THEN 'Answer'
WHEN Field1 < Field2 THEN 'No Answer'
ELSE 'Question'
END AS Field3
FROM MyTable
WHERE Field2 = #Field2

Related

SQL Server : skip update if variable is NOT NULL

I have a problem with a SQL Server database. I want to update the values of an ID only if the value is null, if there is a value it should be skipped -> not updated.
I don't know how to realise this "if else" in SQL Server - could somebody give me a hint?
Thanks in advance.
This is my pseudo code:
UPDATE dbo.mytable
IF FIELD1 IS NOT NULL SKIP --Don't update
ELSE IF FIELD1 IS NULL
SET FIELD1 (SELECT DISTINCT FIELD1
FROM mytable
WHERE {Some ID} = '123'
AND FIELD1 IS NOT NULL)
WHERE {Some ID} = '123'
AND FIELD1 IS NULL
UPDATE t
SET t.FIELD1 = ( SELECT DISTINCT TOP 1 FIELD1
FROM mytable t2
WHERE SomeID = '123'
AND FIELD1 IS NOT NULL )
FROM dbo.mytable t
WHERE FIELD1 IS NULL
AND SomeId = '123'
I think you want something like this:
UPDATE dbo.mytable
SET FIELD1 = ?
WHERE FIELD1 IS NULL;
The ? is a placeholder for the value you want to set it to.
If you want to set the value to the "123" value:
UPDATE dbo.mytable
SET FIELD1 = (SELECT t2.FIELD1 FROM dbo.mytable t2 WHERE t2.ID = 123)
WHERE FIELD1 IS NULL;
You can simply write the below query
UPDATE dbo.mytable
SET ID = 123
WHERE ID IS NULL
Hope this will help you. It will only update the ID when the value in ID field is NULL.
UPDATE dbo.mytable
SET FIELD1 = (SELECT DISTINCT FIELD1
FROM mytable
WHERE {Some ID} = '123'
AND FIELD1 IS NOT NULL)
WHERE {Some ID} = '123'
AND FIELD1 IS NULL --here your IF condition will get filter [[IF FIELD1 IS NOT NULL SKIP --Don't update]]

DRY for field formula in Oracle SQL

We have some SQL in Oracle which looks like this:
SELECT
CASE
WHEN
field1 - field2 + field3 < 0
THEN
0
ELSE
field1 - field2 + field3
END
we'd like to specify field1 - field2 + field3 once, whilst not altering the output of the SELECT query. Is there any neat way to do this?
As an alternative, if there is a function which can return a numeric value above 0 and returns 0 for negative values that would work as well.
Use greatest():
select greatest(field1 - field2 + field3, 0)
Something like this. If you wrap it in a stored procedure you can return the result.
DECLARE
n number := field1 - field2 + field3;
BEGIN
CASE
WHEN
n < 0
THEN
0
ELSE
n
END CASE
END;
Try:
SELECT
CASE
WHEN
expr < 0
THEN
0
ELSE
expr
END
FROM (SELECT field1 - field2 + field3 as expr
FROM ... );

How can I make this pl/sql cursor more efficient?

Hi guys I have a pl/sql cursor that takes too long to execute. I want to know how can I make the same process but with better performance and probably better code. I am new to PL/SQL.
Declare
Cursor Cursor1 is
select * from table1 where
field1 IS NULL
or
field2 IS NULL or field3 IS NULL or field4 is null or field5 IS NULL or field6 IS NULL;
Begin
For i in Cursor1 loop
if i.field1 IS NULL then
update table1 set field1=0 where recordId=i.recordId;
end if;
if i.field2 IS NULL then
update table1 set field2=0 where recordId=i.recordId;
end if;
if i.field3 IS NULL then
update table1 set field3=0 where recordId=i.recordId;
end if;
if i.field4 IS NULL then
update table1 set field4=0 where recordId=i.recordId;
end if;
if i.field5 IS NULL then
update table1 set field5=0 where recordId=i.recordId;
end if;
if i.field6 IS NULL then
update table1 set field6=0 where recordId=i.recordId;
end if;
End loop;
End;
The question basically is how can I update a field of one specific record, taking into account the conditions of the field. The thing is that the update can occur in the same record many times if the condition apply for many fields in the record.
Thanks...
It's possible to do the same with one UPDATE
UPDATE table1 SET
field1 = COALESCE(field1, 0)
, field2 = COALESCE(field2, 0)
, field3 = COALESCE(field3, 0)
, field4 = COALESCE(field4, 0)
, field5 = COALESCE(field5, 0)
, field6 = COALESCE(field6, 0)
WHERE field1 IS NULL OR field2 IS NULL OR field3 IS NULL
OR field4 IS NULL OR field5 IS NULL OR field6 IS NULL
Here's another take on this:
UPDATE TABLE1
SET FIELD1 = NVL(FIELD1, 0),
FIELD2 = NVL(FIELD2, 0),
FIELD3 = NVL(FIELD3, 0),
FIELD4 = NVL(FIELD4, 0),
FIELD5 = NVL(FIELD5, 0),
FIELD6 = NVL(FIELD6, 0);
Rationale: any query which performs this update is going to do a full table scan anyways because it's looking for NULLs, which won't be indexed in the usual case, and even if they ARE indexed there's a fair chance the optimizer will choose a full table scan anyways. Why waste time checking six different fields for NULLs?
Share and enjoy.
Try executing couple of updates like this, avoiding the usage of a cursor:
update table1 set field1=0 where field1 IS NULL;
update table1 set field2=0 where field2 IS NULL;
update table1 set field3=0 where field3 IS NULL;
update table1 set field4=0 where field4 IS NULL;
update table1 set field5=0 where field5 IS NULL;
update table1 set field6=0 where field6 IS NULL;
I don't think that there is a more efficient way to do this.

SQL Inequality in CASE WHEN statement not working due to NULL values

I have a SQL query which includes the following statement
SELECT
(CASE
WHEN field1 > 0 THEN field1
ELSE (field2 * field3)
END) AS result
FROM table
Which is supposed to return the value of field1, unless that is '0', in which case the statement returns the product of two other fields.
The issue I am having is this statement returns NULL if field1 is NULL.
I want to revise my SQL so that the product of fields 2 & 3 is returned when field1 is NULL, just as it does when field1 is '0'.
I have tried the following:
SELECT
(CASE
WHEN field1 > 0 THEN field1
WHEN field1 IS NULL THEN (field2 * field3)
ELSE (field2 * field3)
END) AS result
FROM table
But NULL still seems to permeate through the statement and NULL is still being returned whenever field1 is NULL.
Your query does already what you want. When field1 is NULL, it returns the product field2 * field3. If either of field2 or field3 though is NULL, the (returned) product will be NULL.
If you want to be returning some value (0 perhaps) when either of them is NULL, you can change the CASE:
SELECT
(CASE
WHEN field1 > 0 -- when field1 > 0
THEN field1
WHEN field2 IS NULL OR field3 IS NULL -- when field1 = 0 or NULL
THEN 0
ELSE field2 * field3
END) AS result
FROM table ;
or simply:
SELECT
(CASE
WHEN field1 > 0
THEN field1
ELSE COALESCE(field2 * field3, 0)
END) AS result
FROM table ;
You can use COALESCE / IFNULL (SQL Server / MySQL)
Easy as:
SELECT
COALESCE((CASE
WHEN field1 > 0 THEN field1
ELSE (field2 * field3)
END), 0) AS result
FROM table
To help you diagnose this issue, use something like this:
SELECT
(CASE
WHEN field1 > 0 THEN field1
WHEN field1 IS NULL THEN (field2 * field3)
ELSE (field2 * field3)
END) AS result,
field1, field2, field3
FROM table
WHERE
(CASE
WHEN field1 > 0 THEN field1
WHEN field1 IS NULL THEN (field2 * field3)
ELSE (field2 * field3)
END) IS NULL
It will become immediately apparrent what the problem is.

Conditional statement in WHERE clause

I need to write a conditional statement in my where clause that uses different operators based on the parameter passed into the procedure. I can't seem to find the syntax that will work.
My example is as follows:
#DateValue datetime
select *
from table
where field1 = 'x'
and field2 = 'y'
and if #DateValue = '1/1/1900' then
field3 <= getdate()
else
field3 = #DateValue
end
Thanks for everyone's assistance.
and ((#DateValue = '1/1/1900' and field3 <= getdate()) or
(#DateValue <> '1/1/1900' and field3=#DateValue))
I think you would need something like (psuedo-code)
Create PROCEDURE GetPartialTable
#DateValue datetime
AS
Begin
IF (#DateValue = '1900-01-01')
select * from table where field1 = 'x' and field2 = 'y' and field3 <= getdate();
else
select * from table where field1 = 'x' and field2 = 'y' and field3 = #DateValue;
END IF
END
It sounds like the solution above, but the results might be seriously different if the context is more general, e.g. if the entire SQL statement is completely different.