Does SQL Server take NULL value as an empty string? - sql

I recently read a SQL code snippet which confuses me.
declare #test nvarchar(100) = NULL
select
case
when #test <> '' then 1
else 0
end
I was quite confident that the result will be 1, since I think NULL is not equivalent to an empty string. However, the actual output is 0.
(I'm using MS SQL Server 2012 on Windows 7 64-bit)
As far as I understand, '' is an empty string which indicates the value contains 0 character, and Null means the data is in absence. But now I'm not sure about this. Can anyone help me to sort it out? Is this some exemption case?

When you use NULL for your comparison, it always will return NULL/unknown so, in fact is not true, so is false.
To analyze a NULL field you must use IS NULL
select
case
when #test IS NULL then ....
when #test <> '' then ....
else ....
end
or you can re-write your query as follow:
select
case
when #test IS NULL or #test = '' then ...
when #test <> '' then ....
end

Null doesn't equal ''.
Null is the absent of a value.
Null also doesn't equal Null so SELECT 1 where NULL = NULL will also return nothing.
Use this instead.
declare #test nvarchar(100) = NULL
select case when #test IS NULL then 1
else 0
end

Use something like this:
declare #test nvarchar(100) = NULL
select case when #test <> '' OR #test IS NULL then 1
else 0
end

NULL is not the same as ''. Just like NULL is not the same as 0. NULL is a special value used to indicate that no value of the datatype is being stored.
If you want to COALESCE the NULL to a concrete value, you can use the ISNULL or the COALESCE functions in SQL Server.
DECLARE #test NVARCHAR(100) = NULL
SELECT
CASE
WHEN ISNULL(#test, N'') <> N'' THEN
1
ELSE
0
END

Related

PostgreSQL find blank rows

I want to search for non null values from the 'currentsheet' which works fine but some fields are actually blank rather than null. How can I find blank fields using postgreSQL as the below has not worked and still displays blank values under the 'currentsheet' field.
SELECT *
FROM PUBLIC._http_requests
WHERE (_http_requests.currentsheet IS NOT NULL OR _http_requests.currentsheet <> '')
AND _http_requests.session_id IS NOT NULL
AND _http_requests.http_referer IS NOT NULL
You need to use AND to check _http_requests.currentsheet. If it was NULL, then it would always be true for the <> '' check and vice versa.
As a way simpler example, you can use select statements without a table to help debug this sort of thing (from psql or whatever SQL query tool you like):
select ('' is not null or '' <> '') as empty_result,
(null is not null or null <> '') as null_result;
empty_result | null_result
--------------+-------------
t |
If the string is '', you get true. If the string is null, you get null (this is because comparisons with null are SQL oddities -- select null = null; results in null). Let's see what happens when we replace or with and:
select ('' is not null and '' <> '') as empty_result,
(null is not null and null <> '') as null_result;
empty_result | null_result
--------------+-------------
f | f
Neat! With X is not null and X <> '', we get false when X is either '' or null.
So the way to phrase the select statement to do what you actually want is:
SELECT *
FROM PUBLIC._http_requests
WHERE _http_requests.currentsheet IS NOT NULL
AND _http_requests.currentsheet <> ''
AND _http_requests.session_id IS NOT NULL
AND _http_requests.http_referer IS NOT NULL;
I think you just need AND _http_requests.currentsheet <> ''. The AND is important there so that we exclude both.

trigger sql null datetime variable

Iam writing an sql trigger with sql server
in this clause
IF (#EXON = '' OR #DATEDEB='' OR #DATEFIN= '') AND #N_CATComp =4
#DATEDEB #DATEFIN are datetime variable and the application accept null values for those both variable I want in this case null is not accepted , this works only for the first variable #EXON
plz help
IF (#EXON = '' OR ISNULL(#DATEDEB, '') ='' OR ISNULL(#DATEFIN, '') = '')
if it's null it'll return ''
To check if a variable #MyVar has it's value equal to null you should do this:
if (#MyVar IS NULL)
print '#MyVar has a NULL value'
else
print '#MyVar value is not NULL'
You should also check the documentation on the helper functions NULLIF and ISNULL.
IF (#EXON = '' OR #DATEDEB='' OR #DATEDEB IS NULL OR #DATEFIN= '' OR #DATEFIN IS NULL) AND #N_CATComp =4

Using ISNULL in where Clause doesn't return records with NULL field

Table:
ID AppType AppSubType Factor
1 SC CD 1.0000000000
2 SC CD 2.0000000000
3 SC NULL 3.0000000000
4 SC NULL 4.0000000000
Query:
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and AppSubType = ISNULL(#ast, AppSubType)
Result:
ID AppType AppSubType Factor
1 SC CD 1.0000000000
2 SC CD 2.0000000000
Question:
Shouldn't this query return all 4 records and not just the first 2?
Abviously #ast is null and Isnull would exchange null with other value, so you shouldn't expect #ast to be not null. If your AppSubType is null , so the result become null but AppSubType=null doesn't mean because AppSubType is null is true. Because null is not a value so it cant work with equal.
for your expected result this code will work.
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and (AppSubType = ISNULL(#ast, AppSubType) Or AppSubType is null)
You can write a case condition in where clause as:
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and 1=
case when isnull(#ast ,'') = '' and isnull(AppSubType ,'') = '' then 1
when AppSubType = ISNULL(#ast, AppSubType) then 1
else 0
end
Please understand the behavior of ISNULL explained in below blog.
the very first expression in the isnull function is a column value or the expression of some result.
ISNULL Explored in Detail
The code looks like a search functionality. If the value is not given then replace them with whatever is there in database and if given pull only those records which are matched.

Null or empty check for a string variable

if((isnull(#value,''))='')
I want to know whether the above piece of code works in checking if the variable is null or empty.
Yes, that code does exactly that.
You can also use:
if (#value is null or #value = '')
Edit:
With the added information that #value is an int value, you need instead:
if (#value is null)
An int value can never contain the value ''.
Use This way is Better
if LEN(ISNULL(#Value,''))=0
This check the field is empty or NULL
Yes, you could also use COALESCE(#value,'')='' which is based on the ANSI SQL standard:
SELECT CASE WHEN COALESCE(#value,'')=''
THEN 'Yes, it is null or empty' ELSE 'No, not null or empty'
END AS IsNullOrEmpty
DEMO
Yes, it works. Check the below example. Assuming #value is not int
WITH CTE
AS
(
SELECT NULL AS test
UNION
SELECT '' AS test
UNION
SELECT '123' AS test
)
SELECT
CASE WHEN isnull(test,'')='' THEN 'empty' ELSE test END AS IS_EMPTY
FROM CTE
Result :
IS_EMPTY
--------
empty
empty
123
Try this:
ISNULL(IIF (ColunmValue!='',ColunmValue, 'no units exists') , 'no units exists') AS 'ColunmValueName'
IF (LEN(#value) > 0) PRINT 'Variable is not null and not empty'
You can try
<column_name> is null
in the where clause.
You can try this.....
DECLARE #value Varchar(100)=NULL
IF(#value = '' OR #value IS NULL)
BEGIN
select 1
END
ELSE
BEGIN
select 0
END
declare #sexo as char(1)
select #sexo='F'
select * from pessoa
where isnull(Sexo,0) =isnull(#Sexo,0)

Using Case Statement in SQL with parameter/variable to check for Null values

I am trying to write a SQL Select statement to return records based on a user input through a front end.
I want to write the Select statement like this:
SELECT somefields
FROM sometable
WHERE CASE variable
WHEN 'blank' THEN field IS NULL
ELSE field = field
END
Basically I either want to filter a column to find NULL values or ignore the filter and return all values depending on the value of the variable. I know that the results of the CASE statement is not executable but how can I do this?
When variable is 'blank', the following query will give you rows where field is NULL. When variable is anything else, it will give you all rows:
SELECT somefields
FROM sometable
WHERE
(variable = 'blank' AND field IS NULL)
OR (variable <> 'blank')
You can use NULLIF() (link is for SQL Server, but NULLIF() should be standard):
SELECT somefields
FROM sometable
WHERE field = NULLIF(variable, 'blank')
The following snippet should behave as follows:
when #variable is null, return all rows
when #variable = 'blank', return all rows where field is null or field = 'blank'
otherwise, return rows where #variable equals field
Code snippet:
WHERE 1 = CASE
WHEN #variable is null then 1
WHEN #variable = 'blank' and field is null then 1
WHEN #variable = field then 1
END
SELECT somefields
FROM sometable
WHERE ((variable IS NULL OR variable = 0) OR (variable = field))
WHERE Criteria is apply when variable have value
For Example:
DECLARE #CityName VARCHAR(50)
SET #CityName = NULL
SELECT CityName
FROM City
WHERE ((#CityName IS NULL ) OR (#CityName = CityName ))
When City is null then tables return all rows
I think I get what you're after. Something like this maybe?
SELECT field1,
field2,
CASE variable
WHEN 'blank' THEN NULL
ELSE field3
END as field3
FROM sometable
Think I understand what you mean....for example....
SELECT
House, Postcode
from
SomeTable
where
(House=isnull(#House,House) or (House is null and #House is null))
and
(Postcode=isnull(#Postcode,Postcode) or (Postcode is null and #Postcode is null))
First bit of the conditional where is to use the variable, when present (the isnull bit is to ignore the variable if it's null)
Second bit of the conditional where is in case your evaluative field is null also as effectively fields don't = null they are 'is null'.
Confused? Good. Works on what I'm doing though!
Here is my solution based on #Andomar answer above aimed at anyone testing an input varchar value, you need to test the parameter in the right order as per the example below:
FIELD1 = CASE
WHEN #inputParameter = '' THEN FIELD1
WHEN #inputParameter <> FIELD1 THEN NULL -- if input is some text but does not match
WHEN #inputParameter IS NULL THEN FIELD1
WHEN #inputParameter != '' AND FIELD1 = #inputParameter THEN FIELD1
END
Hope this helps someone.