Null or empty check for a string variable - sql

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)

Related

Data not coming when old value is changed from null to other value

Select statement is not displaying data when I change value from 0 to null or vice versa.
But when I change data from 0 to 1 select statement starts displaying data.
Please find my code (query)
declare #tmptable TABLE (Id INT, IsVal BIT)
INSERT Into #tmptable VALUES(1,0)
--SELECT * FROM #tmptable
DECLARE #Id INT
DECLARE #IsVal BIT
SET #Id=1
SET #IsVal=NULL
select #Id as PrimaryKeyValue
,CAST(IsVal as VARCHAR) as OldValue,CAST(ISNULL(#IsVal,'') as VARCHAR) as NewValue
,'IsVal' AS DisplayFieldName,
CASE IsVal
WHEN 1 THEN 'Yes'
WHEN 0 THEN 'No'
END as DisplayOldValue
,CASE #IsVal
WHEN 1 THEN 'Yes'
WHEN 0 THEN 'No'
END as DisplayNewValue
from #tmptable WHERE Id =#Id and ISNULL(IsVal,'')<>ISNULL(#IsVal,'')
There is problem with statement :-
ISNULL(IsVal,'')<>ISNULL(#IsVal,'')
Can't figure out the change I need to make to the above statement so that my query can work as I desired. Please help.
#IsVal variable is used to set value.
Thanks
You are correct, the problem is this expression: ISNULL(IsVal,'')<>ISNULL(#IsVal,'')
When IsVal is 0 and #IsVal is NULL, this becomes:
0 <> ''
which will compare as integers, so the '' becomes 0 and you get:
0 <> 0
So, in your case, 0, '' and NULL will all be treated as equal. You need to choose an invalid int (maybe -1?) or cast the 0 to a varchar to do that comparison.
Thanks for the help,
I was able to resolve the issue:-
declare #tmptable TABLE (Id INT, IsVal BIT)
INSERT Into #tmptable VALUES(1,0)
--SELECT * FROM #tmptable
DECLARE #Id INT
DECLARE #IsVal BIT
declare #oldval VARCHAR(10)
SET #Id=1
SET #IsVal=null
select #oldval=isval from #tmptable WHERE Id=1
print ISNULL(#oldval,'')
print ISNULL(#IsVal,'')
--if(ISNULL(CAST(#oldval AS INT),'')<>ISNULL(CAST(#IsVal AS INT),''))
--BEGIN
PRINT '1'
select #Id as PrimaryKeyValue
,CAST(IsVal as VARCHAR(10)) as OldValue,CAST(ISNULL(#IsVal,'') as VARCHAR(10)) as NewValue
,'IsVal' AS DisplayFieldName,
CASE IsVal
WHEN 1 THEN 'Yes'
WHEN 0 THEN 'No'
END as DisplayOldValue
,CASE #IsVal
WHEN 1 THEN 'Yes'
WHEN 0 THEN 'No'
END as DisplayNewValue
from #tmptable WHERE Id =#Id and ISNULL(CAST(IsVal AS VARCHAR),'null')<>ISNULL(CAST(#IsVal AS VARCHAR),'null')
Why not just use a simple comparison?
(IsVal = #IsVal or IsVal IS NULL AND #IsVal IS NULL)
You may still have problems with type conversion, assuming the types are not compatible.
Why not just use a simple comparison?
(IsVal = #IsVal or IsVal IS NULL AND #IsVal IS NULL)
You may still have problems with type conversion, assuming the types are not compatible.
If that is the case:
(IsVal = CONVERT(VARCHAR(255), #IsVal) or
IsVal IS NULL AND #IsVal IS NULL
)
There is no need to invent special values for isnull() or coalesce().

Does SQL Server take NULL value as an empty string?

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

SQL Filter SELECT statement

I need to filter my select statement to only grab a value is a bit value is true. For example
DECLARE
#Value AS NVARCHAR(20) = 'Some Value'
#BIT AS BIT = 1,
#Stuff AS NVARCHAR(20) = 'Stuff'
SELECT
#Stuff,
IF #BIT = 1 BEGIN #Value END ELSE BEGIN '' END
Something like this basically. I only want to select the value if its true. I have multiple checkboxes and the data should only pull that data back if the user selects one of those check boxes so I'm using bit's to track if its checked or not. Thanks in advance
Use CASE not IF:
SELECT
#Stuff,
CASE WHEN #BIT = 1 THEN #Value ELSE '' END
or
CASE #BIT WHEN 1 THEN #Value ELSE '' END
It's a little unclear what you are trying to do, so I will give you several approaches:
If you are limiting your select statement, you can do:
SELECT ...
FROM ...
WHERE myBitField = 1
or, if your bit field is a variable that's being passed in, you can do:
SELECT ...
FROM ...
WHERE myBitField = #BIT
If you don't want to limit your select results, you can use a case statement:
SELECT CASE WHEN #BIT = 1 THEN #Value ELSE '' END as BitResult
FROM ...

SQL stored procedure variable null checking

Below is part of a stored procedure that I have, it would be of great help if you can point me why is that even when #DefaultID is null after executing the select statement it does not enter the if condition, is there are different method I have to follow to assign the value to #DefaultID for it to be available for null checking.
Thank you in advance.
DECLARE #DefaultID varchar(25)
DECLARE #ISDefault varchar(1)
SELECT #DefaultID = (SELECT TABLE1.DEFID as DEFID
FROM TABLE1
WHERE TABLE1.ID = '123')
IF (#DefaultID = '')
SET #ISDefault = '1'
ELSE
SET #ISDefault = '0'
Use
IF #DefaultID IS NULL
Instead of IF #DefaultID =''
NULL and '' are two different things.
simply use this :-
IF(#DefaultID is NULL)
Please check using
IF ISNULL(#DefaultID, '') = '' instead of IF(#DefaultID = '')
I guess you are using MS Sql server.
IF(len(#DefaultID) > 0)
SET #ISDefault = '1'
ELSE
SET #ISDefault = '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.