I have a query that looks at two times and calculates the difference and puts the value (in seconds) in another field. I then sort that field. The problem is when one of the times is empty the row in the new field in not populated so when I do the sort the rows that nothing in them are pushed to the top.
What I would like to do is replace the null with another value. Something like 9999999. That way when I perform the sort the rows that now have 999999 will be place at the bottom of the sort.
Here is the SQL Expression for the query.
SELECT FOCFClassic.FirstName, FOCFClassic.LastName, FOCFClassic.[Bib#], FOCFClassic.[2011SDStartTime], FOCFClassic.[2011SDFinishTime], Diff2Dates("ns",[FOCFClassic].[2011SDStartTime],[FOCFClassic].[2011SDFinishTime]) AS 2011SDRunTime, FOCFClassic.SDCategory, FOCFClassic.Team, FOCFClassic.SDRank, DateDiff("s",[2011SDStartTime],[2011SDFinishTime]) AS TotalTime
FROM FOCFClassic
WHERE (((Diff2Dates("ns",[FOCFClassic].[2011SDStartTime],[FOCFClassic].[2011SDFinishTime]))<>"") AND ((DateDiff("s",[2011SDStartTime],[2011SDFinishTime])) Is Not Null))
ORDER BY FOCFClassic.SDRank, DateDiff("s",[2011SDStartTime],[2011SDFinishTime]);
I'm a noob at this so a little hand holding my be needed.
Thanks is advance!
Gordon
You can either use the NZ() function: NZ(MyDate, #1/1/1950#) will return jan 1st 1950 if the field is null, the field value otherwise. You can achieve the same result using IIF() and ISNULL() functions or IS NULL condition.
In terms of performance, using IIF(myDate IS NULL, #1/1/1950#, myDate) should be the fastest.
Related
I have database ready with data as per below screenshot, green column is customized column which I need to generate while querying data from SQL/Oracle.
Logic: If Actual_Completion_Date is not an empty/null, then bring
Actual_Completion_Date into Completion_Date else get
Schedule_Completion_Date in Completion_Date column.
Is there any way, where I should write If statement while defining column names in SQL query without stored procedure help.
If both date field contains NULL value then use simply COALESCE(). IF first one is NULL then second one value will show if it's not NULL. If first one is not null then will sshow first one value.
SELECT Activity_Details, Actual_Completion_Date
, Schedule_Completion_Date
, COALESCE(Actual_Completion_Date, Schedule_Completion_Date) AS Completion_Date
FROM tbl;
You can simply do this in the SELECT clause of your query. For example using the IF() function like this in mysql:
SELECT Activity_Details, Actual_Completion_Date, Schedule_Completion_Date, IF(Actual_Completion_Date IS NOT NULL, Actual_Completion_Date, Schedule_Completion_Date) AS Completion_Date
FROM tbl;
The IF function takes a condition that should return True or False as the first argument. If the condition evaluates to true, the second argument is returned and if it evaluates to false, the third.
In Oracle or Microsoft SQL server you would do something similar in the SELECT clause of your query, but using CASE WHEN ... THEN ... ELSE ... END
Oracle (and MySQL) both support generated columns. That means that you can add the logic as part of the table definition. I'm not sure if this is what you are asking for, but in Oracle, this looks like:
alter table t add column completion_date date generated always as
(coalesce(Actual_Completion_Date, Schedule_Completion_Date)) virtual;
This would be calculated when the table is queried and available to any query that uses the table.
Hello I am trying to select the smallest value between 3 columns with MySQL.
The issue I am facing is that in a column I might have NULL value. How can I get the least value that is not NULL?
Running select least(1, 3, NULL) will return NULL but I would like to have 1 as an answer.
Thank you for your help.
The way least() handles nulls is a documented behavior:
If any argument is NULL, the result is NULL. No comparison is needed.
That might seem annoying, but propagating the null value to the resultset is how your database signals you that one of your values is undefined; this is consistent with the behavior of other operations that involve null (string concatenation, arithmetic operations, ...).
If only the third column may be null, and none of the other two, you could do:
least(col1, col2, coalesce(col3, col1))
I have 2 tables with "like" fields and am simply trying to "flag" all records where the "like" fields are different. For example, I have a description field in table #1 and a description field in table #2.
I created a new field titled Description_Diff with the formula Descr_diff: IIf([Tbl 1 items].[description]<>[Tbl 2 items].[description],"diff","").
The issue I am having is that if one of the two values is Null/Blank it does not return a "diff". I apologize in advance if this answer had already been covered, but upon searching the site was unable to find the answer.
Try with Nz:
Descr_diff: IIf(Nz([Tbl 1 items].[description])<>Nz([Tbl 2 items].[description]),"diff",Null)
The real answer is that you can't compare Null using any logical operator.
So If 1 <> Null doesn't work as a comparison.
Have a read here http://allenbrowne.com/casu-11.html and http://allenbrowne.com/casu-12.html for a fuller explanation.
use coalesce
usage:
coalesce(value, defaultValue)
when comparing if it's null, throw in "" or -1 as a default value, then comparing with blank or default would return false
Use the coalesce in your query in the select
Select coalesce(id,-1), coalesce(firstname,"") From myTable
I'd like to limit my query to show only rows where a certain field is not empty. I found this thread where someone posed the same question and was told to use IS NOT NULL. I tried that, but I'm still getting rows where the field is empty.
What is the correct way to do this? Is Null the same thing as Empty in SQL/MySQL?
My query, if you're interested is:
SELECT * FROM records WHERE (party_zip='49080' OR party_zip='49078' OR party_zip='49284' ) AND partyfn IS NOT NULL
I got it by using AND (partyfn IS NOT NULL AND partyfn != '')
When comparing a NULL value, the result in most cases becomes NULL and therefor haves the same result as 0 (the FALSE value in MySQL) inside WHERE and HAVING.
In your given example, you don't need to include IS NOT NULL. Instead simply use party_zip IN ('49080', '49078', '49284'). NULL can't be 49080, 49078, 49284 or any other number or string.
What you do need to think about though, is when checking for empty values. !party_zip won't return TRUE/1 if the value is NULL. Instead use OR columns IS NULL or !COALESCE(party_zip, 0)
Can anyone help me understand or post any ideas concerning this where clause?
sql was here
I've changed the table name, but other than that, any idea what the developer was trying to do here?
There is nothing else after that, that's the where clause.
If (table.date_field = (select max(table2.exit_date) from table as table2)) is null the it'll return 1=1, which basically means there's no where clause at all.
Now let's look into that nasty expression. I can only assume that if "a = b" is not true then that's also equivalent to null, otherwise it seems like the first branch would always happen. It looks like it's trying to say "if the latest exit date is equal to the date field, select those, otherwise have no where clause". However, I don't think that this will work at all. It really looks like either way, each row will be selected.
The MySQL ifnull function returns the first argument if it is not null, otherwise the second argument. This looks like it tries to compare table.date_field to the max(table2.exit_date), and return true if the comarison was not possible due to nulls.
It looks to me like he is trying to find the row where table.date_field is equal to the maximum of table.exit_data. There is a check for null which I think would happen in any of these cases:
table is empty
all rows in table have exit_data set to NULL
table.date_field is NULL for the row in question
In any of these three cases, the row will be returned. I don't understand why he uses the string '1=1' instead of, to give some examples: 1=1, 1 or true, but it appears to work fine. In the first case I assume that there will be no rows in the result set anyway (depending on the rest of the query) so he was probably trying to handle one of the other two cases - I'd guess the last one.
This is only an explanation of what is happening. To understand why he is doing this, it would help if you gave a little more context.
MySQL is nonstandard in that true is really equal to the numeric value 1. Any expression that evaluates to true, or any nonzero value, satisfies the condition.
mysql> CREATE TABLE foo AS SELECT 1=1 AS f;
mysql> SHOW CREATE TABLE foo;
CREATE TABLE `foo` (
`f` INT NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
So the following WHERE clause is legal in MySQL, but not in most other SQL databases:
... WHERE 1;
Some people use 1=1 as a placeholder condition meaning true, but putting it in a string is meaningless because SQL expressions have no equivalent to an eval() function as other languages have. In this case, the leading character 1 in the string is implicitly cast to a numeric value 1, which is interpreted as true in MySQL. So it probably works as intended, but kind of by accident.
The use of IFNULL() is so that if either date_field or MAX(exit_date) is NULL, it returns the row. If you didn't use this function, then anything = NULL would evaluate as unknown, which means the row would not be returned.
It says basically if table.date_field = max exit date or if max exit_date is null or table.date_field is null return true. Will return false if max exit_date is not null and table.date_field is not null but they do not equal.