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))
Related
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
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
Many times I have to run this query:
select * from users where name is not null and name != ''
Is there any better way to do this. I need more performance, any suggestion. I guess this is very common, so there may be some compiled function which will be something like
select * from users where name is present()
Using PostgreSQL 9 version.
For any x, null != x will be null and null is not true. That means that you can simply ignore NULLs in your case and say:
select * from users where name != ''
You could also convert NULLs to empty strings using COALESCE if that's clearer to you:
select * from users where coalesce(name, '') != ''
Of course that coalesce call isn't free.
Demo: http://sqlfiddle.com/#!2/e810c/2
You can use NULLIF:
SELECT *
FROM USERS
WHERE NULLIF(NAME,'') IS NOT NULL
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.
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