Oracle SQL Case statement with NULL values - sql

I'm trying to create an Oracle query that will use a parameter based on the CommandName of a button.
So here's my query
SELECT id
FROM table
WHERE dateTime IS (CASE WHEN :CommandName = 'FIRST' THEN NULL ELSE NOT NULL END)
So I'm passing the parameter but it's not working - I'm just getting a Missing NULL keyword error
I basically don't want to have to write two separate queries based on the parameter that is input
Not even sure if this is possible or if this is the right way of doing it?
Any ideas?

You may want to read up on the CASE statement a bit more.
I believe you want the following code:
SELECT
id
FROM
table
WHERE
(
(dateTime IS NULL AND :CommandName = 'FIRST')
OR
(dateTime IS NOT NULL AND NVL(:CommandName, '') <> 'FIRST')
)

Without using dynamic SQL to return the string 'NULL' or 'NOT NULL' you could use two NVL() functions:
select id
from table
where NVL(dateTime,'FIRST') = NVL(:param,dateTime);
However, if dateTime is indexed (and Oracle can index NULLs), ths NVL() function will disable the index.

I think that
SELECT id
FROM table
WHERE (dateTime IS NULL AND :CommandName='FIRST') OR (dateTime IS NOT NULL AND :CommandName <> 'FIRST')
should do what you need.

Related

t-sql query returns undefined after using ORDER BY

I am currently working with a MS SQL database on Windows 2012 Server
I need to query only 1 column from a table that I only have access to read, not make any kind of changes.
Problem is that the name of the column is "Value"
My code is this:
SELECT 'Value' FROM table
If I add
`ORDER BY 'Value'`
The issue is that the query is returning an empty list of results.
Things I've tried already
I tried replacing ' with `"' but this didn't work either.
I also tried writing SELECT * instead of SELECT VALUE
Using the table name in the SELECT or ORDER clauses again didn't help
You are claiming that this query:
SELECT 'Value'
FROM table
ORDER BY 'Value'
Is returning no rows. That's not quite correct. It is returning an error because SQL Server does not allow constant expressions as keys for ORDER BY (or GROUP BY for that matter).
Do not use single quotes. In this case:
SELECT 'Value' as val
FROM table
ORDER BY val;
Or, if value is a column in the table:
SELECT t.Value
FROM table t
ORDER BY t.Value;
Value is not a reserved word in SQL Server, but if it were, you could escape it:
SELECT t.[Value]
FROM table t
ORDER BY t.[Value];
it looks like your table has null values. and because of the order by all null values come first.
try to add filter like this
select Value FROM table
where Value is not null and Value <> ''
order by Value

Selecting from table to insert into another, getting a type error

I have the following query which inserts data into one table after selecting it from another.
The problem is that the data types do not match for one of the columns. I have simplified the query below.
INSERT INTO tbl.LogTable (
[SelPartNo], -- This does not match, see below
)
SELECT TOP 1
IF([SelPartNo] = 'False', NULL, [SelPartNo],
FROM tbl.MyTable
WHERE ID = '20358'
ORDER BY CreateDate DESC
The first SelPartNo is an int and the second is a VarChar. In most instances the SelPartNo for the second one (tbl.MyTable) is NULL or an integer, which I don't think will cause a problem. But in some cases the value is "False", which needs to return NULL.
I have tried an IF statement but I am doing something wrong because it's giving a syntax error and I am unsure if this is the correct approach.
Your code is syntactically incorect...
Try it with
NULLIF([SelPartNo],'False')
This function returns NULL if the two expressions are equal.
Details: https://msdn.microsoft.com/en-us/library/ms177562.aspx
I don't think IF is a function, at least not one which you can use in a SELECT statement. But CASE WHEN ... END is your friend:
INSERT INTO tbl.LogTable (
[SelPartNo]
)
SELECT TOP 1
CASE WHEN [SelPartNo] = 'False' THEN NULL ELSE [SelPartNo] END
FROM tbl.MyTable
WHERE ID = '20358'
ORDER BY CreateDate DESC

How to display Items that equal a value in one column and are not null in another

Let's say I want to display all items in a table with following criteria how would i do this?
SELECT *
FROM TABLE
WHERE TABLE.COLUMN1 = 'example' AND TABLE.COLUMN2 != 'NULL'
I want it to display all values from COLUMN1. How does one go about this process in MS SQL?
NULL values can be compared using IS [NOT] NULL in SQL server. Please check this.
SELECT *
FROM TABLE
WHERE TABLE.COLUMN1 = 'example' AND TABLE.COLUMN2 IS NOT NULL
NULL is an UNKNOWN value , you cannot use any Comparison Operators (= , <> , > , <) with it. you check for nulls like
ColumnName IS NULL or ColumnName IS NOT NULL
If you think about it , it makes sense, to compare two or more values, you need to know the values only then you can compare them, Since SQL Server considers a NULL to be an UNKNOWN value, you cannot really compare an unknown value to anything.

Query SQL Server with IN (NULL) not working

When I define a "User-Defined Table Type", as:
CREATE TYPE [dbo].[BitType] AS TABLE(
[B] [bit] NULL
)
I place 0 and null in this table-variable.
Then I do this query:
SELECT something FROM theTable WHERE item IN #theBitTypeTable
Will only get item=0 not item is null
Simply put: SELECT something FROM theTable WHERE item IN (0, NULL) is not working (no error although)
It has to be SELECT something FROM theTable WHERE item=0 OR item IS NULL
So, my question is, if I like to use User-Defined Table Type, but I also need to use NULL value. How can I perform the query correctly to get result include null item.
Thanks (btw, I use MS SQL Server 2008 R2)
The only valid comparison operations with NULL values are IS NULL or IS NOT NULL, others always return false (actually - Unknown, see the #Damien_The_Unbeliever's comment)
So, try the following
CREATE TYPE [dbo].[BitType] AS TABLE(
[B] [tinyint] NOT NULL
)
GO
declare #theBitTypeTable BitType
insert #theBitTypeTable
VALUES(0), (2 /* instead of NULL*/)
SELECT something FROM theTable WHERE IsNull(cast(item as tinyint), 2) IN (select B from #theBitTypeTable)
Null does not equal null in SQL Server (and most other database management systems). You would need to do a coalesce on the joined column, and use a sentinel value to represent nulls.
There is a cheat use isnull on the item being compared.
eg
SELECT something
FROM theTable
WHERE ISNULL(item,0) IN (0)

Select rows where column is null

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