mysql ifnull where - sql

i need such query
select * from t where
field=ifnull(:param, field) 'it not work's
so if param=NULL i have
select * from t where field is NULL
but if param =4
i have
select * from t where field=4

You can use the case when in where clause AFAIK bot not sure about MySQl,
But the better approach is to translate them,
you can read about that SQL WHERE clauses: Avoid CASE, use Boolean logic
So
select * from t where (:param is null and filed is null) or (filed = :param)

You can try this alternative
this might help you
select * from t where (field = NULL AND param= NULL) OR field ='4'

When working with NULL you cannot use arithmetic operators. Try COALESCE to make a logical if with values integer or NULL
mysql> SELECT COALESCE(NULL,1);
-> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
-> NULL

I think you are looking for NULLIF instead of ifnull
I think better approach would be to use CASE in where clause in your case.

Related

Issue with ISNULL error on postgreSQL query [duplicate]

In MS SQL-Server, I can do:
SELECT ISNULL(Field,'Empty') from Table
But in PostgreSQL I get a syntax error. How do I emulate the ISNULL() functionality ?
SELECT CASE WHEN field IS NULL THEN 'Empty' ELSE field END AS field_alias
Or more idiomatic:
SELECT coalesce(field, 'Empty') AS field_alias
Use COALESCE() instead:
SELECT COALESCE(Field,'Empty') from Table;
It functions much like ISNULL, although provides more functionality. Coalesce will return the first non null value in the list. Thus:
SELECT COALESCE(null, null, 5);
returns 5, while
SELECT COALESCE(null, 2, 5);
returns 2
Coalesce will take a large number of arguments. There is no documented maximum. I tested it will 100 arguments and it succeeded. This should be plenty for the vast majority of situations.
How do I emulate the ISNULL() functionality ?
SELECT (Field IS NULL) FROM ...
Try:
SELECT COALESCE(NULLIF(field, ''), another_field) FROM table_name

How to link multiple SELECT statements wth an OR operator

I would need a SELECT statement which returns a Boolean value based on the result of two independent Boolean columns (connected with 'OR'). Does anybody have the syntax for this?
If your database supports boolean, then you can simply put the or expression in the select:
select t.*, (bool_col1 or bool_col2) as new_bool_col
from t;
Select case when (boolean condition) then column name from table name.
You can add multiple case condition to fetch the columns of conditional basis
in MySQL you can use IF() Function, like this:
SELECT IF(bool1 = 'true' OR bool2 = 'true', 'true', 'false')
FROM Yourtable;
Thanks, the comment of jarlh solved it: "Simply select col1 or col2 from tablename"

Rule out blank and null values in WHERE clause

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

Remove NULL from VARCHAR2 in ORACLE

There is a VARCHAR2 type column in my database table for store invoice_number. and there is a query to get max invoice_number as below.
select max(invoice_number) from invoice;
but there are no data in invoice table, above query return value as null. but in this case i need to replace this value to 0 instead of null. how could i do this ?
Using NVL:
SELECT NVL(MAX(invoice_number), '0')
Using the ANSI COALESCE:
SELECT COALESCE(MAX(invoice_number), '0')
Using DECODE:
SELECT DECODE(MAX(invoice_number), NULL, '0')
Using the ANSI CASE:
SELECT CASE
WHEN MAX(invoice_number) IS NULL THEN '0'
ELSE MAX(invoice_number)
END
Verdict
All work, I'd probably use NVL because COALESCE is ANSI but not known to necessarily work as fast as native alternatives.
You could use COALESCE
For instance:
select COALESCE(max(invoice_number),'0') from invoice;
select coalesce(max(invoice_number),0)
can also use Decode.
select decode(max(invoice_number),null, 0)

How to select an empty result set?

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
I'm using a stored procedure in MySQL, with a CASE statement.
In the ELSE clause of the CASE ( equivalent to default: ) I want to select and return an empty result set, thus avoiding to throw an SQL error by not handling the ELSE case, and instead return an empty result set as if a regular query would have returned no rows.
So far I've managed to do so using something like:
Select NULL From users Where False
But I have to name an existing table, like 'users' in this example.
It works, but I would prefer a way that doesn't break if eventually the table name used is renamed or dropped.
I've tried Select NULL Where False but it doesn't work.
Using Select NULL does not return an empty set, but one row with a column named NULL and with a NULL value.
There's a dummy-table in MySQL called 'dual', which you should be able to use.
select
1
from
dual
where
false
This will always give you an empty result.
This should work on most DBs, tested on Postgres and Netezza:
SELECT NULL LIMIT 0;
T-SQL (MSSQL):
SELECT Top 0 1;
How about
SELECT * FROM (SELECT 1) AS TBL WHERE 2=3
Checked in myphp, and it also works in sqlite and probably in any other db engine.
This will probably work across all databases.
SELECT * FROM (SELECT NULL AS col0) AS inner0 WHERE col0 IS NOT NULL;
SELECT TOP 0 * FROM [dbo].[TableName]
This is a reasonable approach to constant scan operator.
SELECT NULL WHERE FALSE;
it works in postgresql ,mysql, subquery in mysql.
How about this?
SELECT 'MyName' AS EmptyColumn
FROM dual
WHERE 'Me' = 'Funny'
SELECT * FROM (SELECT NULL) WHERE 0
In PostgreSQL a simple
SELECT;
works. You won't even get any columns labeled 'unknown'.
Note however, it still says 1 row retrieved.