I have the following case statement, the AppointmentDate isn’t always populated and therefore require to pull from other date fields - looking to have one appointment date (populated from many fields)
When running this statement, doesn’t return full results.
,Case=Appointment
When AppointmentDate Is Null Then ChaseDate
When ChaseDate Is Null Then DueDate
Else AppointmentDate
End
Any thoughts on where I’ve went wrong?
Thanking you in advance.
You haven't stated if this is supposed to be an expression in SSRS or this is part of your dataset query.
Assuming SQL Server here...
If this is part of your dataset query then you can do something like
SELECT
FieldA, FieldB,
myAppointmentDate = COALESCE(AppointmentDate, ChaseDate, DueDate)
FROM myTable
COALESCE will return the first non-null value
If you are trying to do this in an SSRS expression then you need to use the SWITCH() statement.
=SWITCH(
Fields!AppointmentDate.Value <> Nothing, Fields!AppointmentDate.Value,
Fields!ChaseDate.Value <> Nothing, Fields!ChaseDate.Value,
Fields!DueDate.Value <> Nothing, Fields!DueDate.Value,
True, Fields!AppointmentDate.Value
)
This will return the first expression that is true, if non of the first three exression return true, the final True acts like an else (which in your case would return Nothing anyway)
Why does SELECT IF(null=null, 'true', 'false'); return false but SELECT IF(1=1, 'true', 'false'); return true in mysql?
NULL in SQL is a placeholder for an unknown value. Because it is unknown it cannot be compared to other values, not even with NULL.
Read "unknown" every time you encounter NULL in a query and it will make more sense.
Regarding MySQL, the behaviour of NULL is documented: https://dev.mysql.com/doc/refman/5.7/en/working-with-null.html
I am working on a SSIS Package that will populate a SharePoint 2013 list with data from other SP lists.
I have created a Derived Column in the package, which is intended to replace null based on dates that are greater than or equal to a specific date in the data list. However, I am having trouble with the expression. Below is a condition and expression example that I am have trouble with.
REPLACENULL(ColumnName,"mm/dd/yyyy" > = GETDATE())
Any assistance to point out what I am doing wrong is appreciated.
Use this expression:
((DT_DATE)"9-1-2016") >= (DT_DATE)ColumnName ? NULL((DT_WSTR, 50)) : ColumnName
Notice: REPLACENULL is not useful for you here. What REPLACENULL does is that it:
Returns the value of second expression parameter if the value of first expression parameter is NULL". (See here)
You don't want to replace NULL, you want NULL!
Syntax for REPLACENULL is REPLACENULL(expression 1,expression 2) where
expression 1 :
The result of this expression is checked against NULL.
expression 2 :
The result of this expression is returned if the first expression evaluates to NULL.
If you're trying to REPLACE NULL values of a column based on another column's datetime condition, try something like this :
[column_1] >= GETDATE() ? REPLACENULL([column_2],"desired value") : [column_2]
I've a query like this one:
SELECT IF(#param = 42, 'static', SELECT ... );
But it doesn't work because I can't insert a SELECT statement inside a IF(). My problem is that I can't do otherwise (use an if-then statement outside sql) because to "architecture restrictions".
Any solution to select if evaluate or not a query based to parameter value?
You don't need a select:
select (case when #param = 42 then 'static' else date_format(now(), '%Y-%m-%d') end)
Note that you are trying to mix two different types -- a datetime and string. You should explicitly convert the datetime to a string, using your preferred format.
You can write this with if(). case is slightly more general and the ANSI standard.
SELECT IFNULL(NULL, 'Replaces the NULL')
--> Replaces the NULL
SELECT COALESCE(NULL, NULL, 'Replaces the NULL')
--> Replaces the NULL
In both clauses the main difference is argument passing. For IFNULL it's two parameters and for COALESCE it's multiple parameters. So except that, do we have any other difference between these two?
And how it differs in MS SQL?
The main difference between the two is that IFNULL function takes two arguments and returns the first one if it's not NULL or the second if the first one is NULL.
COALESCE function can take two or more parameters and returns the first non-NULL parameter, or NULL if all parameters are null, for example:
SELECT IFNULL('some value', 'some other value');
-> returns 'some value'
SELECT IFNULL(NULL,'some other value');
-> returns 'some other value'
SELECT COALESCE(NULL, 'some other value');
-> returns 'some other value' - equivalent of the IFNULL function
SELECT COALESCE(NULL, 'some value', 'some other value');
-> returns 'some value'
SELECT COALESCE(NULL, NULL, NULL, NULL, 'first non-null value');
-> returns 'first non-null value'
UPDATE: MSSQL does stricter type and parameter checking. Further, it doesn't have IFNULL function but instead ISNULL function, which needs to know the types of the arguments. Therefore:
SELECT ISNULL(NULL, NULL);
-> results in an error
SELECT ISNULL(NULL, CAST(NULL as VARCHAR));
-> returns NULL
Also COALESCE function in MSSQL requires at least one parameter to be non-null, therefore:
SELECT COALESCE(NULL, NULL, NULL, NULL, NULL);
-> results in an error
SELECT COALESCE(NULL, NULL, NULL, NULL, 'first non-null value');
-> returns 'first non-null value'
Pros of COALESCE
COALESCE is SQL-standard function.
While IFNULL is MySQL-specific and its equivalent in MSSQL (ISNULL) is MSSQL-specific.
COALESCE can work with two or more arguments (in fact, it can work with a single argument, but is pretty useless in this case: COALESCE(a)≡a).
While MySQL's IFNULL and MSSQL's ISNULL are limited versions of COALESCE that can work with two arguments only.
Cons of COALESCE
Per Transact SQL documentation, COALESCE is just a syntax sugar for CASE and can evaluate its arguments more that once. In more detail: COALESCE(a1, a2, …, aN)≡CASE WHEN (a1 IS NOT NULL) THEN a1 WHEN (a2 IS NOT NULL) THEN a2 ELSE aN END. This greatly reduces the usefulness of COALESCE in MSSQL.
On the other hand, ISNULL in MSSQL is a normal function and never evaluates its arguments more than once. COALESCE in MySQL and PostgreSQL neither evaluates its arguments more than once.
At this point of time, I don't know how exactly SQL-standards define COALESCE.
As we see from previous point, actual implementations in RDBMS vary: some (e.g. MSSQL) make COALESCE to evaluate its arguments more than once, some (e.g. MySQL, PostgreSQL) — don't.
c-treeACE, which claims it's COALESCE implementation is SQL-92 compatible, says: "This function is not allowed in a GROUP BY clause. Arguments to this function cannot be query expressions." I don't know whether these restrictions are really within SQL-standard; most actual implementations of COALESCE (e.g. MySQL, PostgreSQL) don't have such restrictions. IFNULL/ISNULL, as normal functions, don't have such restrictions either.
Resume
Unless you face specific restrictions of COALESCE in specific RDBMS, I'd recommend to always use COALESCE as more standard and more generic.
The exceptions are:
Long-calculated expressions or expressions with side effects in MSSQL (as, per documentation, COALESCE(expr1, …) may evaluate expr1 twice).
Usage within GROUP BY or with query expressions in c-treeACE.
Etc.
Differences in SQL-Server:
There is no IFNULL() function but a similar ISNULL()
ISNULL takes only 2 parameters whereas COALESCE takes variable number of parameters
COALESCE is based on the ANSI SQL standard whereas ISNULL is a proprietary TSQL function
Validations for ISNULL and COALESCE is also different. For example, NULL value for ISNULL is converted to int, whereas for COAELSCE you have to provide a type. Ex:
ISNULL(NULL,NULL) : is int.
COALESCE(NULL,NULL) : will throw an error.
COALESCE(CAST(NULL as int),NULL) : is valid and returns int.
Data type determination of the resulting expression – ISNULL uses the first parameter type, COALESCE follows the CASE expression rules and returns type of value with highest precedence.
ifnull can only replace a null value of the first parameter. Whereas coalesce can replace any value with another value. With coalesce in standard SQL you can have many parameters transforming many values.
EDIT the example according to comments below.
Example: coalesce(null, null, null, 'b*', null, 'null*')
returns 'b*' and it is not possible to do with ifnull.
This db2 SQL will not work with COALESE, I will not see any rows retrieved.
Since I used IFNULL it is working as expected
select a.mbitno ,a.mbstqt,ifnull(b.apr,0)
from
(
select mmstcd,mbstat,mbfaci,mbwhlo,mbitno,mbstqt,MBALQT from libl.mitbal inner join libl.mitmas on
mmcono=mbcono and mmitno=mbitno
where mbcono=200 and mbstat in ('20','50') and mmstcd>0
)
as a left join
(
select mlfaci,mlwhlo,mlitno,mlstas,sum(mlstqt) as APR from libl.mitloc where mlcono=200 and mlstas='2'
group by mlfaci,mlwhlo,mlitno,mlstas
)
b on b.mlfaci=a.mbfaci and b.mlwhlo=a.mbwhlo and b.mlitno=a.mbitno
where a.mbitno in 'GWF0240XPEC' and a.mbstqt>0 and a.mbstqt<>ifnull(b.apr,0)