SQL Functions
This web page describes what a SQL function are, and shows you how to use them. SQL functions are pass-by-value functions. That means you put copies of material into the function as call parameters, the function consumes the original values, and returns a processed output value. In the purest programming sense SQL functions are factories that produce what are known as expressions.
SQL functions come in two varieties, those delivered as part of the database management system engine and those created through procedural languages supported by the same engine. These functions perform many utilities inside SQL statements, like changing and/or formatting one data type into another, performing complex mathematical resolutions (dates), or supplying flow control to a non-procedural language.
This page shows you that process by showing you how to handle null values. Null values are special in SQL. They use what is known as three-value logic, which differs from two-value logic where what isn’t true is false and vice versa. It also shows you how to cast data types from one type to another with SQL functions.
Three-valued logic ↓
Three-value logic is only a conundrum until somebody explains it. There’s a nice explaination that I’ve borrowed from the Oracle Database 11g PL/SQL Workbook. Click on the Three-valued Logic title if you’re interested in understanding it.
“Three-valued logic means basically that if you find something is true when you look for truth, it is true. By the same token, when you check whether something is false and it is, then it is false. The opposite case isn’t proved. That means when something isn’t true, you can’t assume it is false, and vice versa.
The third case is that if something isn’t true, it can be false or null. Likewise, if something isn’t false, it can be true or null. Something is null when a Boolean variable is defined but not declared or when an expression compares something against another variable that is null.”
Null Values ↓
Null values can occur in two principle places in DML statements. They can be in filtering clauses, like the WHERE clause. They can also be returned as potential column values from a query. This section describes how to work with null values in both places.
Null Value Filters ↓
You filter data through the WHERE clause. The WHERE clause provides the ability to compare a column value against another column or literal value by using the = (equal) comparison operator. If the two values are equal the statement resolves to true and when they’re not false.
You can also use the >, >=, <, and <= inequality operators to make relative comparisons of inequality. These also resolve to true or false. On the other hand, It doesn’t support comparing a column value against a null. That’s because a null isn’t equal to anything. Therefore, a comparison with a mathematical operator returns NULL not necessary true or false.
This statement demonstrates a no row return result that you may think supports comparing a value to a null value. Note that the FROM dual clause for Oracle isn’t required in MySQL or SQL Server.
SELECT 'True' FROM dual WHERE 'Something' = NULL;
However, you can see the problem if we test the contrary position that it is false. This statement uses the <> (not equal) operator to test that something isn’t equal to null. It returns no rows too.
SELECT 'True' FROM dual WHERE 'Something' <> NULL;
You might try a combination of the prior two statements to return the 'True' string. It also returns no rows. The question lies in the idea of whether or not something is true in a three-valued logic model. The easy answer is that the comparative statement returns a NULL, which makes the entire statement false.
SELECT 'True' FROM dual WHERE 'Something' = NULL AND 'Something' <> NULL;
The standard math symbols for equality and inequality don’t work. You check whether a column value is null by using the IS or IS NOT operator. It checks whether the reference to see if it points to a data stream or a null value (typically a null terminator).
You use the following syntax to check if a column value contains a null value:
WHERE some_column IS NULL
You use the following syntax to check if a column value doesn’t contain a null value:
WHERE some_column IS NOT NULL
Null Value Functions ↓
There isn’t a convenient way to substitute a value for a null value in a result set, except by calling the implementation dependent function.
There isn’t a convenient way to substitute a value for a null value in a result set, except by calling the implementation dependent function.
The beginning of the page makes more sense if one reads the second paragraph first. Once I read that, I could understand what was going on in the first paragraph. The first paragraph talks about functions, but the concept of functions is not delineated until the second paragraph. The description of a concept’s use is useless until after the concept has been defined. That is how I read things, at least. Not sure I’m really qualified to comment…
Sam Johansen
4 Feb 10 at 1:22 pm
I concur. It’s switched now.
michaelmclaughlin
4 Feb 10 at 8:42 pm
“There isn’t a convenient way to substitute a value for a null value in a result set, except by calling the implementation dependent function.”
Is COALESCE not an implementation independent function and is there a reason we would not want to use it over the functions you mentioned (NVL, ISNULL, etc.)?
Ryan D
17 May 10 at 2:21 pm
The most portable is:
The
COALESCEfunction is used to find the first null value in a list of values.michaelmclaughlin
17 May 10 at 2:30 pm