In SQL or T-SQL how can I go through a table and set fields that are NULL to empty strings if the type is textual and 0 if the fields are integer types.
Thanks for any help, I do not even know where to begin.
Well if it's just one table use a basic update query:
UPDATE MyTable
SET NumericColumn = ISNULL(NumericColumn,0),
StringColumn= ISNULL(StringColumn,'')
What you're suggesting would only work as a "one off".
If you always want those fields to be populated going forward you can set a default value on the table.
You can use IsNull function in T-SQL,
Syntax :
ISNULL ( check_expression , replacement_value )
The function coalesce( expression_1 , expression_2 , ... ) is also useful in this context.
It evaluates the specified expressions from left to right and returns the value of the first expression that evaluates to a value other than null, or null if all the specified expressions evaluate to null.
This can be useful, especially in conjunction with left joins and table rotations.
Related
Actually I tried to run a is null SQL Statement on a SAP HANA Database using the SAP HANA Studio. This does not work because SQLScript has no is not null or is null function. My statement looks like:
Select *
From MSEG
Where KDAUF is null
Unfortunately it does not work. Does anybody know an alternative approach which is practicable using SAP HANA SQLScript? On the internet I found a hint to either use NULLIF or COALESCE. But I neither know how to use this function nor to adapt it to a working WHERE condition.
NULLIF ( expression , expression )
Returns the same type as the first expression.
NULLIF returns the first expression if the two expressions are not equal. If the expressions are equal, NULLIF returns a null value of the type of the first expression
WHERE IFNULL(KDAUF , comparison value) I never use this one
The other is coalesce where if the first vaLue is null, the second VALUE is returned:
WHERE VALUEa = COALESCE(KDAUF,valuea)
here, if kdaUf Is null, coalesce will will return valuea, the default if the first value is null. Since valuea = valuea, the where clause will be true, which is just another way of validating that KDAUF IS NULL
Try to use it this way
Select * from MSEG Where KDAUF **NE** ''
Why doesn't the following code work in SQL
SELECT *
FROM DATA
WHERE VALUE != NULL;
we can not Compare null value with = We Special operator to compare null
value in sql
IS OPERATOR
SELECT *
FROM DATA
WHERE VALUE is not NULL;
Null is not any Value.Sql consider Null as Unknown/absence of data.
The condition you written is not in proper format.
If you want to select not null values from your table then you can use the following command
select *from table name where column name IS NOT NULL
If should work if you replace "!=" with "IS NOT".
Because a NULL value indicates an absence of data, it doesn't make sense to compare something with a value to it with an equals operator.
You can make your SELECT statement work by setting ANSI_NULLS OFF
SET ANSI_NULLS OFF
before running the statement. You can get further information here:
https://msdn.microsoft.com/en-us/library/ms188048.aspx
Null does not contain any value. Null is neither zero nor any value.
So we can't compare it using comparision operators.
Instead of using '=', you should use 'IS' keyword.
SELECT * FROM DATA WHERE VALUE IS NOT NULL;
The code will not work in SQL because it is not possible to test for NULL values with the following operators =, <, or <>. It is not possible to compare NULL and 0 as they are not equivalent.
You can test for NULL values using the IS NULL and IS NOT NULL operators instead.
SELECT *
FROM DATA
WHERE VALUE IS NOT NULL
http://www.w3schools.com/sql/sql_null_values.asp
Is this the best way to select default value from the table if selecting value is 0 or null?
DECLARE #value INT = 15
DECLARE #defaultValue INT = 12
SELECT IIF(ISNULL(#value,0) = 0, #defaultValue, #value)
Specify "best". Since IIF works only in SQL-Server i'd use CASE which is ANSI SQL standard and works in every(?) rdbms:
SELECT CASE WHEN ISNULL(#value,0) = 0 THEN #defaultValue ELSE #value END
Actually IIF is even translated to CASE:
IIF is a shorthand way for writing a CASE expression ...
The fact that IIF is translated into CASE also has an impact on other
aspects of the behavior of this function....
But the same is true for ISNULL which is also a SQL-Server function and could be replaced by COALECSE.
By the way, if you use ISNULL or COALESCE in a WHERE-clause, it prevents the query optimizer from using an index. So then you should prefer:
SELECT ...
FROM dbo.TableName
WHERE #value IS NOT NULL AND #value <> #value
However, i prefer ISNULL over COALESCE since the latter has an issue if it contains a sub-query. It is executed twice whereas ISNULL executes it once. Actually COALESCE is also translated into CASE. You can read about that issue here.
You can use the COALESCE. It evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL. It is used for this purpose to get the first not null value.
SELECT COALESCE(#value,#defaultValue)
But keep in mind,
If all arguments are NULL, COALESCE returns NULL. At least one of the
null values must be a typed NULL.
You can also use the ISNULL but there is difference between both of them that is as listed below,
Comparing COALESCE and ISNULL
1) The ISNULL function and the COALESCE expression have a similar
purpose but can behave differently. Because ISNULL is a function, it
is evaluated only once. As described above, the input values for the
COALESCE expression can be evaluated multiple times.
2) Data type
determination of the resulting expression is different. ISNULL uses
the data type of the first parameter, COALESCE follows the CASE
expression rules and returns the data type of value with the highest
precedence.
3) The NULLability of the result expression is
different for ISNULL and COALESCE. The ISNULL return value is
always considered NOT NULLable (assuming the return value is a non-nullable one) whereas COALESCE with non-null parameters is
considered to be NULL. So the expressions ISNULL(NULL, 1) and
COALESCE(NULL, 1) although equivalent have different nullability
values. This makes a difference if you are using these expressions in
computed columns, creating key constraints or making the return value
of a scalar UDF deterministic so that it can be indexed as shown in
the following example.
If you are planning to use the sub queries in the expression to check for the NULL then you better of using the ISNULL as COALESCE will evaluate same query multiple times.
It can be a lot of arguments what is the best way and what it not, but
for me it is slightly more readable when using case syntax:
select case when #value is null then #defaultValue else #value end
You can use CASE statement.
SELECT CASE WHEN #value IS NULL THEN #defaultValue ELSE #value END
Or COALESCE expression
SELECT COALESCE(#value,#defaultValue)
I am having problem while understanding = and is operators in SQL Server.
Consider the following example queries which are having different behaviors in their respective output:
SELECT * FROM tableName WHERE colName IS NULL;
SELECT * FROM tableName WHERE colName = NULL;
First query will provide the required output i.e. select those records for which colName is having a null value. But the second query will result in zero matching records.
Please clarify different uses of these operators with pros and cons.
EDIT
Here, most of the answers are claiming that = doesn't work with null, but the following statement will work with null and =.
SET ANSI_NULLS OFF
SELECT * FROM tableName WHERE colName = NULL;
This will provide the same result as statement having is operator.
Nothing equals null.
Not even null equals null.
null is not a value, it is more like a concept, or a mark, meaning unknown value.
As such, you need two operators for this, one for equality, and one for checking the concept of null.
Once you start to think of null as "unknown value" a lot of the other behavior also makes sense.
10 + null? Add an unknown value to 10? Obviously you will have another unknown value as a result.
For more information, please check the documentation of the equality operator in T-SQL.
Additionally, see the documentation for SET ANSI_NULL.
Note that the documentation is in conflict about the behavior of x = null between the equality operator (documentation says it will always be false if x is non-null) whereas SET ANSI_NULLS documentation says that x = null will behave equivalent to x is null when the option is turned on.
From http://msdn.microsoft.com/en-us/library/ms188795.aspx:
To determine whether an expression is NULL, use IS NULL or IS NOT NULL instead of comparison operators (such as = or !=). Comparison operators return UNKNOWN when either or both arguments are NULL.
EDIT
The originating question was updated to note that SET ANSI_NULLS OFF allows:
select * from tableName where colName = null.
This may be true at the moment but future versions of SQL server will set ANSI_NULLS always ON and any calls to SET ANSI_NULLS OFF will result in an error.
Source: http://msdn.microsoft.com/en-us/library/ms188048.aspx
= NULL is always unknown (this is piece of 3 state logic), but WHERE clause treats it as false and drops from the result set. So for NULL you should use IS NULL
= NULL is used for assignment to a NULL value whereas IS NULL is used to determine whether a variable is NULL-valued.
See these articles on null
Wikipedia NUll (SQL)
w3schools SQL NULL Values
SQL Tutorial, see IS NULL Operator section
Null is not same as zero. Null is absence of value. It simply means that the value could be anything. It is unknown.
Q. Why select * from tableName where colName = null returns zero rows?
A. Because you can not be sure that one null(unknown value) is equal to or not equal to another null(another unknown value).
EG:
I have a magic hat and nobody knows what will come out of it(if anything comes out at all).
You have another magic hat and nobody knows what will come out of it(if anything comes out at all).
We both have a magic hat each and what it has inside is unknown (null).
These both hats could contain a rabbit each or may be my hat contains a hammer and your's has a pineapple.
If you have an if condition like this..
if(#flag<>null)
it is a mistake because if you pass null to #flag you do not really know whether or not #flag is equal or not equal to null
Use if(#flag<>isnull(null,'')) instead
In my table, I have a nullable bit column (legacy system...) and another developer recently made a change to a stored procedure to only show values where the bit column was not true (1). Because this is a nullable column, we noticed that if the column was NULL, the record was not being picked up. WHY is this?
Both the other developer and I agree that NULL <> 1... Is this a bug in SQL or was this designed this way? Seems like a design flaw.
Current Code:
(VoidedIndicator <> 1)
Proposed Fix:
(VoidedIndicator <> 1 OR VoidedIndicator IS NULL)
Clarification (By Jon Erickson)
VoidedIndicator is a nullable bit field so it can have the following values: NULL, 0, or 1
When a SQL statement is created with a where clause such as (VoidedIndicator <> 1) we only get records returned that have VoidedIndicator == 0, but we were expecting both VoidedIndicator == 0 and VoidedIndicator IS NULL. Why is this?
Lots of good answers, but let me give you a really concise version.
To SQL, Null does NOT mean "No value" it means "Unknown Value"
With that in mind, consider the answer to the question you are asking SQL in plain English.
Q: Is this unknown value not equal to 1?
A: I don't know, there is no way to tell without knowing the value.
Hence Null<>1 = Null
From the Wikipedia entry on NULL:
For example, a WHERE clause or
conditional statement might compare a
column's value with a constant. It is
often incorrectly assumed that a
missing value would be "less than" or
"not equal to" a constant if that
field contains Null, but, in fact,
such expressions return Unknown. An
example is below:
-- Rows where num is NULL will not be returned,
-- contrary to many users' expectations.
SELECT * FROM sometable WHERE num <> 1;
Basically, any comparison between NULL and something else, whether it's with = or <> will not be true.
As another reference, the MSDN T-SQL page on <> states:
Compares two expressions (a comparison
operator). When you compare nonnull
expressions, the result is TRUE if the
left operand is not equal to the right
operand; otherwise, the result is
FALSE. If either or both operands are
NULL, see SET ANSI_NULLS
(Transact-SQL).
The SET ANSI_NULLS page then states:
When SET ANSI_NULLS is ON, a SELECT
statement that uses WHERE column_name
= NULL returns zero rows even if there are null values in column_name. A
SELECT statement that uses WHERE
column_name <> NULL returns zero rows
even if there are nonnull values in
column_name.
...
When SET ANSI_NULLS is ON, all
comparisons against a null value
evaluate to UNKNOWN. When SET
ANSI_NULLS is OFF, comparisons of all
data against a null value evaluate to
TRUE if the data value is NULL.
It's not a bug.
NULL is not equal to anything, not even NULL (NULL = NULL returns FALSE).
Typically NULL values aren't indexed either. It's generally a bad idea to rely on a particular value or NULL. Depending on what you're storing in the column, you may be better off putting a dummy or sentinel value in rather than using NULL to indicate some meaning.
The other folks are correct that NULL <> 1 doesn't evaluate as true, therefore it doesn't satisfy the WHERE clause.
The proposed fix you describe is the best way of handling it:
(VoidedIndicator <> 1 OR VoidedIndicator IS NULL)
SQL-99 does have a predicate that helps in this case, called IS DISTINCT FROM:
(VoidedIndicator IS DISTINCT FROM 1)
This predicate would behave exactly the same as your proposed fix. Unfortunately, Microsoft SQL Server does not support IS DISTINCT FROM yet.
You can also: isnull(VoidedIndicator,1) <> 1
Because the WHERE clause only selects rows when the condition evaluates to true.
When one of the operands is NULL, the condition usually evaluates to UNKNOWN (approximately equivalent to NULL), and therefore is not true. It applies to both 'column = 1' and 'column <> 1'; if column is NULL, the search condition fails.
It is why you get told to avoid NULL columns whenever possible.
NULL <> 1 evaluates (theoretically) to "maybe", which means the record will not be returned.