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
Related
I need to show a set of string into new column based on some columns value.
I tried to use isnull (name, variable = ''null) but does not work
DECLARE #NAME VARCHAR (MAX)
DECLARE #TEL VARCHAR (MAX)
SET #NAME = '- inNAME '
SET #TEL = '- inTEL '
SELECT IIF(NAME IS NULL, #NAME = '- notinNAME'), IIF(TEL IS NULL, #TEL = '- notinTEL;), (#NAME+ #TEL) AS RESULT FROM ADDRESS
NAME TEL RESULT
NULL 07 - notinNAME - inTEL
ARK NULL - inNAME - notinTEL
ARK 07 - inNAME - inTEL
NULL NULL - notinNAME - notinTEL
Is this what you are trying to do?
SELECT CONCAT(CASE WHEN NAME IS NULL THEN '- notinNAME' ELSE '' END,
CASE WHEN TEL IS NULL THEN '- notinTEL' ELSE '' END
)
FROM ADDRESS;
You cannot use variables for a few reasons. First, SQL Server does not allow you to assign variables in a SELECT and also to return values.
More importantly, SQL (in general) does not guarantee the order of evaluation of expressions in the SELECT. Your formulation assumes a particular order of evaluation, so it is not going to work.
EDIT:
If you want to use the variables:
SELECT CONCAT(CASE WHEN NAME IS NULL THEN #NAME ELSE '' END,
CASE WHEN TEL IS NULL THEN #TEL ELSE '' END
)
FROM ADDRESS;
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
I have a case inside my stored procedure which I use before executing the data.
DECLARE #Setup nvarchar(50)
SELECT
#ZipCode = CASE
WHEN REPLACE(#ZipCode, '0', '') = ''
THEN NULL ELSE #ZipCode
END,
#ZipCodeEND = CASE
WHEN REPLACE(#ZipCodeEND, '0', '') = ''
THEN NULL ELSE #ZipCodeEND
END,
SELECT
#Setup = CASE WHEN (LEN(ISNULL(#ZipCode, ''))) > 0 THEN '1' ELSE '0' END +
CASE WHEN (LEN(ISNULL(#ZipCodeEND,''))) > 0 THEN '1' ELSE '0' END
IF ISNULL(#ID, 0) = 0
BEGIN
INSERT INTO dbo.MapToStaticValuesTable(ZipCode, ZipCodeEND, Setup)
VALUES(#ZipCode, #ZipCodeEND, #Setup)
END
The problem here is even if zipcode and zipcodeEnd are empty and set to null after being saved into the table I keep getting the value "11" instead of getting "00".
Now if I do the same example with nvarchar values it would work, but since ZipCode and ZipCodeEnd are set to int it's acting weird.
It is acting weird because you are using string functions on integers. Not sure what you are trying to achieve with your code but I'm sure it can be done just by checking the values as integers.
I guess this could be what you are looking for.
select case when nullif(#ZipCode, 0) is null then '0' else '1' end +
case when nullif(#ZipCodeEND, 0) is null then '0' else '1' end
One example of weird
select isNull(#ZipCode, '')
return 0 if #ZipCode is null.
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)
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'