SQLite NOT NULL - sql

How can I make this exclude the row if any of the (stage1_choice, stage2_choice or stage3_choice) values = null ?
I've tried:
SELECT r.breakout_id ,
r.case_id ,
r.stage_id ,
r.chart_id ,
s.stage1_choice ,
s.stage2_choice ,
s.stage3_choice
FROM Results AS r ,
Submissions AS s
WHERE r.breakout_id = '1'
AND s.breakout_id = '1'
AND r.case_id = s.case_id
AND stage_id < 4
AND s.stage1_choice NOT NULL
AND s.stage2_choice NOT NULL
AND s.stage3_choice NOT NULL
But it still returns rows that contain a null in one of the columns.

The correct syntax for not null is different, and it's IS NOT NULL
AND s.stage1_choice IS NOT NULL
AND s.stage2_choice IS NOT NULL
AND s.stage3_choice IS NOT NULL
From documentation
To test for NULL, use the IS NULL and IS NOT NULL operators
Learn more here

try
SELECT r.breakout_id, r.case_id, r.stage_id, r.chart_id, s.stage1_choice, s.stage2_choice, s.stage3_choice
FROM Results as r, Submissions as s
WHERE r.breakout_id = '1' AND s.breakout_id = '1' AND r.case_id = s.case_id AND stage_id < 4 AND s.stage1_choice NOT NULL AND s.stage2_choice IS NOT NULL AND s.stage3_choice IS NOT NULL
or
SELECT r.breakout_id, r.case_id, r.stage_id, r.chart_id, s.stage1_choice, s.stage2_choice, s.stage3_choice
FROM Results as r, Submissions as s
WHERE r.breakout_id = '1' AND s.breakout_id = '1' AND r.case_id = s.case_id AND stage_id < 4 AND s.stage1_choice != '' AND s.stage2_choice != '' AND s.stage3_choice != ''

Related

SQL query to return 0 or 1 as one record from result of two queries

I have a RunHistory table, where process run result logs in. Process is either running thru a scheduler where it logs in RunInstance as Numeric, but if run manually, then it logs in 'MANUAL'
I need a query to check if the process ran today (GetDate()) regardless it was MANUAL or thru scheduler. If it ran, then query should return 1 else 0 as one single record. I have created temp table and UNION query to demo the issue.
create table #RunHistory
(
[RunId] [int] IDENTITY(1,1) NOT NULL,
[ReportDate] [date] NOT NULL,
[RunInstance] [varchar](6) NOT NULL,
[RunStartTime] [datetime] NOT NULL,
[RunEndTime] [datetime] NULL,
)
INSERT INTO #RunHistory
([ReportDate]
,[RunInstance]
,[RunStartTime]
,[RunEndTime])
VALUES
('2020-07-29'
,'1200'
,'2020-07-29 12:44:13.340'
,'2020-07-29 12:44:25.313')
INSERT INTO #RunHistory
([ReportDate]
,[RunInstance]
,[RunStartTime]
,[RunEndTime])
VALUES
('2020-07-29'
,'MANUAL'
,'2020-07-29 12:36:51.117'
,'2020-07-29 12:41:10.720')
--if both ran returing 1 then it works fine
SELECT RESULT
FROM
(
SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS RESULT
FROM #RunHistory
WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
AND (ISNUMERIC(RunInstance) > 0)
AND CONVERT(TINYINT,LEFT(RunInstance,2)) >= 8
AND CONVERT(DATE,#RunHistory.ReportDate) = CONVERT(DATE,Getdate())
UNION
SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS RESULT
FROM #RunHistory
WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
AND RunInstance ='MANUAL'
AND CONVERT(DATE,#RunHistory.ReportDate) = CONVERT(DATE,Getdate())
) as z
--if one of then returns 0 as result, then two records are returned. I just want if one of the records is 1, then return 1
SELECT RESULT
FROM
(
SELECT 0 AS RESULT
FROM #RunHistory
WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
AND (ISNUMERIC(RunInstance) > 0)
AND CONVERT(TINYINT,LEFT(RunInstance,2)) >= 8
AND CONVERT(DATE,#RunHistory.ReportDate) = CONVERT(DATE,Getdate())
UNION
SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS RESULT
FROM #RunHistory
WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
AND RunInstance ='MANUAL'
AND CONVERT(DATE,#RunHistory.ReportDate) = CONVERT(DATE,Getdate())
) as z
Don't know if I really need a UNION or somehow both conditions can be combined in one query OR an outer query is required.
You shouldn't need to restrict by RunInstance if it doesn't matter whether it was manual or not:
select case when count(1) > 0 then 1 else 0 end result
from #RunHistory
where RunEndTime is not null
and RunInstance is not null
and convert(date,ReportDate) = convert(date,getdate())
If you do need to union multiple queries, then you can get the maximum result:
select max(result)
from (
SELECT 0 AS RESULT
FROM #RunHistory
WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
AND (ISNUMERIC(RunInstance) > 0)
AND CONVERT(TINYINT,LEFT(RunInstance,2)) >= 8
AND CONVERT(DATE,ReportDate) = CONVERT(DATE,Getdate())
UNION
SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS RESULT
FROM #RunHistory
WHERE RunEndTime IS NOT NULL AND RunInstance IS NOT NULL
AND RunInstance ='MANUAL'
AND CONVERT(DATE,ReportDate) = CONVERT(DATE,Getdate())
) as z

Compare String in Oracle Case When

i have an issue with oracle case when.
SELECT CASE WHEN '7C54D3E133830A78E040A8C010014B7D' != ''
THEN '7C54D3E133830A78E040A8C010014B7D'
WHEN 'e84a4433966c4b8996ce34905acff63d' != ''
THEN 'e84a4433966c4b8996ce34905acff63d'
WHEN '7faa9126b1c6412fa58375ab2b2be1db' != ''
THEN '7faa9126b1c6412fa58375ab2b2be1db'
ELSE NULL
END
FROM DUAL
this query always returns null, though it's obvious the result should be the first case. Am i missing something about string comparison in oracle?
You are checking strings againts an empty string, thus having issues; in Oracle you'd better check if your string is not null:
SELECT CASE WHEN '7C54D3E133830A78E040A8C010014B7D' is not null
THEN '7C54D3E133830A78E040A8C010014B7D'
WHEN 'e84a4433966c4b8996ce34905acff63d' is not null
THEN 'e84a4433966c4b8996ce34905acff63d'
WHEN '7faa9126b1c6412fa58375ab2b2be1db' is not null
THEN '7faa9126b1c6412fa58375ab2b2be1db'
ELSE NULL
END
FROM DUAL
About the way Oracle treats empty string and null, here you find something more
An example:
select q'['' = '']' , case when '' = '' then 'YES' else 'NO' end from dual union all
select q'['' is null]' , case when '' is null then 'YES' else 'NO' end from dual union all
select q'['' = null ]' , case when '' = null then 'YES' else 'NO' end from dual union all
select q'[null = null]' , case when null = null then 'YES' else 'NO' end from dual union all
select q'[null is null]' , case when null is null then 'YES' else 'NO' end from dual union all
select q'['' != '']' , case when '' != '' then 'YES' else 'NO' end from dual union all
select q'['' is not null]' , case when '' is not null then 'YES' else 'NO' end from dual union all
select q'['' != null ]' , case when '' != null then 'YES' else 'NO' end from dual union all
select q'[null != null]' , case when null != null then 'YES' else 'NO' end from dual union all
select q'[null is not null]', case when null is not null then 'YES' else 'NO' end from dual
gives:
'' = '' NO
'' is null YES
'' = null NO
null = null NO
null is null YES
'' != '' NO
'' is not null NO
'' != null NO
null != null NO
null is not null NO
In a word, the only check you can rely on, when talking about NULL, is:
IS [NOT] NULL
Well, the reason of such a behaviour is that Oracle doesn't have empty string, but null; that's why
select case when 'abc' != ''
....
is actually
select case when 'abc' != null
and since anything != null is null (true, false, null boolean logic) all the when don't return true and else is executed.
The right syntax is
SELECT CASE WHEN '7C54D3E133830A78E040A8C010014B7D' IS NOT NULL
THEN '7C54D3E133830A78E040A8C010014B7D'
WHEN 'e84a4433966c4b8996ce34905acff63d' IS NOT NULL
THEN 'e84a4433966c4b8996ce34905acff63d'
WHEN '7faa9126b1c6412fa58375ab2b2be1db' IS NOT NULL
THEN '7faa9126b1c6412fa58375ab2b2be1db'
ELSE NULL
END
FROM DUAL

need to select records from table based on whether a variable is null or not. If null then check for not equal to condition

This may sound easy for the folks working on sql on a daily basis this ones eaten lot of my time.
I have table from which i need to select records based on variable value if value is null set
status column to not equal to 3, is variable value is not equal to null filter based on the variable value..
DECLARE #status int
set #status = 1
SELECT change_set_history_id, files_changed, is_previewed
FROM dbo.ChangeSetHistory WITH (NOLOCK)
WHERE [user_name] = 'djacob' AND
[culture] = 'nl-NL' AND
(#status IS NULL AND [status] <> 3) OR (#status IS NOT NULL AND [status] = #status) AND
[approver] = null AND
is_deleted = 0
if it try with this part query it works
DECLARE #status int
set #status = 1
select * from ChangeSetHistory where status = #status and user_name = 'djacob' and culture = 'nl-NL'
Just wrap your OR condition with another pair of parentheses:
SELECT change_set_history_id, files_changed, is_previewed
FROM dbo.ChangeSetHistory WITH (NOLOCK)
WHERE [user_name] = 'djacob' AND
[culture] = 'nl-NL' AND
((#status IS NULL AND [status] <> 3) OR (#status IS NOT NULL AND [status] = #status)) AND
[approver] IS null AND
is_deleted = 0
Without if your condition is equivalent to:
WHERE ([user_name] = 'djacob' AND [culture] = 'nl-NL' AND (#status IS NULL AND [status] <> 3)) OR
((#status IS NOT NULL AND [status] = #status) AND [approver] = null AND is_deleted = 0)
Test case:
DECLARE #status int
SET #status = 1
SELECT *
FROM (
SELECT NULL [status] UNION ALL
SELECT 1 [status] UNION ALL
SELECT 2 [status] UNION ALL
SELECT 3 [status]
) T
WHERE ((#status IS NULL AND [status] <> 3) OR (#status IS NOT NULL AND [status] = #status));
Other problem i see is [approver] = null condition. It sohould be [approver] is null
Try to not mix AND and OR in the same context.
You missed global parentheses on the status condition :
Instead of
(#status IS NULL AND [status] <> 3) OR (#status IS NOT NULL AND [status] = #status)
It must be
((#status IS NULL AND [status] <> 3) OR (#status IS NOT NULL AND [status] = #status))
It should resolve your problem:
DECLARE #status int
set #status = 1
SELECT change_set_history_id, files_changed, is_previewed
FROM dbo.ChangeSetHistory WITH (NOLOCK)
WHERE [user_name] = 'djacob' AND
[culture] = 'nl-NL' AND
((#status IS NULL AND [status] <> 3) OR (#status IS NOT NULL AND [status] = #status)) AND
[approver] = null AND
is_deleted = 0

Sql Server Where Case Then Is Null Else Is Not Null

I have a procedure which receive a bit variable called #FL_FINALIZADA.
If it is null or false I want to restrict my select to show only the rows that contain null DT_FINALIZACAO values. Otherwise I want to show the rows containing not null DT_FINALIZACAO values.
Something like this:
SELECT
*
FROM
MyTable
WHERE
...
AND
(
OPE.DT_FINALIZACAO = (
CASE
WHEN (#FL_FINALIZADA <> 1)
THEN NULL
END
) OR
OPE.DT_FINALIZACAO IS NOT NULL
)
In this case I receive the message:
None of the result expressions in a
CASE specification can be NULL.
How can I achieve this?
Thanks in advance.
SELECT
*
FROM
MyTable
WHERE
(ISNULL(#FL_FINALIZADA, 0) = 0
AND
OPE.DT_FINALIZACAO IS NULL
)
OR
(#FL_FINALIZADA = 1
AND
OPE.DT_FINALIZACAO IS NOT NULL
)
Change the AND to be:
AND (((#FL_FINALIZADA <> 1) AND (OPE.DT_FINALIZACAO IS NULL)) OR ( (#FL_FINALIZADA = 1) AND (OPE.DT_FINALIZACAO IS NOT NULL)))
If the bit flag is 1 then DT_FINALIZACAO can't be null.
IF #FL_FINALIZADA IS NULL
SET #FL_FINALIZADA = 0
SELECT * FROM NewsletterSubscribers
WHERE
(#FL_FINALIZADA = 0 AND OPE.DT_FINALIZACAO IS NULL)
OR
(#FL_FINALIZADA = 1 AND OPE.DT_FINALIZACAO IS NOT NULL)
My detailed SQL is a little rusty, but have you tried using 0 insted of NULL? I would expect 0 to evaluate the same as NULL in that select

checking if condition in stored procedure( sql server 2005)

i have an SP where i need to check for if condition
ALTER PROCEDURE [dbo].[spCheck]
#strEmpname VARCHAR(50),
#intReturn INT OUTPUT,
#intWorkdID INT,
#intEmpID INT
AS
BEGIN
IF(#intWorkdID is not null and #intWorkdID != '')
BEGIN
IF EXISTS ( SELECT *
FROM Employee
WHERE [Empname] = #strEmpname
AND WorkID = #intWorkdID
)
SELECT #intReturn = '1'
END
ELSE
IF(#intEmpID is not null and #intEmpID != '')
BEGIN
IF EXISTS ( SELECT *
FROM Employee
WHERE [Empname] = #strEmpname
AND PeopleID = #intEmpID
)
SELECT #intReturn = '1'
END
ELSE IF(#intEmpID is not null and #intEmpID != '')
and(#intWorkdID is not null and #intWorkdID != '')
BEGIN
SELECT #intReturn = '0'
END
END
here based on WorkID,EmpID
1 condition and 2 condition should execute
if both condition fail i need to excute the third condition
can any one tell the syntax for it
thanks
prince
Best way is that you can use
Try something as below:
SELECT #intReturn =
CASE
WHEN #intWorkdID IS NULL THEN 1
WHEN #intWorkdID<>'' THEN 1
WHEN #intEmpID IS NULL THEN 1
WHEN #intEmpID <>'' THEN 1
ELSE 0
END
Case... When
for this
An int can't equal ''.
I'm not sure what you're asking for in your logic, but when an if doesn't match then it runs the else part. You can then have anpther if -> else after that as per your script.