i an new in vb.ner sql and what i am asking may be silly question. I have a table in sql server 2005 and a column name activated. this column contain NULL, true or false.
I want to select only NULL or false values. How can i do this?
The selection should be done on the SQL Server's side. Your query should look like this:
SELECT *
FROM MyTable
WHERE activated = 0 OR activated is NULL
The above assumes that the column activated is of an integral or a BIT data type.
Note: it may be tempting to use a seemingly equivalent WHERE activated <> 1 condition. This would be incorrect, though, because comparisons of NULL values to anything result in a NULL, so the rows with activated = NULL would be excluded.
You should be able to do a coalesce on the field to get it to false if it is null. A coalesce statement checks to see if the first parameter is null, if it is it returns the value in the second parameter. There would be two solutions:
SELECT *
FROM MyTable
WHERE COALESCE(activated,'false') <> 'true'`
--OR--
SELECT *
FROM MyTable
WHERE activated = 'false' or activated is null
With SQL's 3-valued logic, comparisons for NULL need to be done using the IS NULL operator... the check for false can be done with =:
SELECT YourColumn
FROM YourTable
WHERE
YourColumn IS NULL
OR YourColumn = 0
SELECT * FROM MyTable WHERE isnull(activated, 0) = 0
For checking NULL you have to use the keyword "IS NULL" and for false =0 like below
SELECT * FROM MyTable WHERE activated = 0 OR activated is NULL
Sometimes you have to see it to believe...
PRINT '0 = 0' ; IF 0 = 0 PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT '0 = 1' ; IF 0 = 1 PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT '0 = NULL' ; IF 0 = NULL PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT '1 = NULL' ; IF 1 = NULL PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT 'NULL = NULL' ; IF NULL = NULL PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT 'NULL IS NULL'; IF NULL IS NULL PRINT 'true' ELSE PRINT 'false';PRINT '';
The SQL way is to use IS TRUE or similar:
SELECT * FROM MyTable WHERE activated = 0 IS NOT FALSE
Related
Why does this give me 1 which is what I was expecting:
IF (SELECT 123) = 123
PRINT 1
ELSE
PRINT 2
But this gives me 2 which I was not expecting:
IF (SELECT NULL) = NULL
PRINT 1
ELSE
PRINT 2
NULL values are checked by IS NULL
you have to use:
IF (SELECT NULL) IS NULL
PRINT 1
ELSE
PRINT 2
from the manual:
To search for column values that are NULL, you cannot use an expr =
NULL test. The following statement returns no rows, because expr =
NULL is never true for any expression
If you put NULLS OFF
SET ANSI_NULLS OFF
IF (SELECT NULL) = NULL
PRINT 1
ELSE
PRINT 2
then you will get PRINT 1
You cant check NULL with =. For that IS has to be used.
Ex:
IF (SELECT NULL) IS NULL
PRINT 1
ELSE
PRINT 2
You cannot check NULL using =. You need to use IS NULL like the following
IF (SELECT NULL) IS NULL
PRINT 1
ELSE
PRINT 2
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.
I'm trying to write his in sql:
SET `Field` = (
If `Field` is empty then "b",
if `Field` is not empty, concat(`Field`,"_b"))
Meaning, if Field had value a it would change to a_b, if it was empty, it would change to b
Is there an sql synthax that creates this if..else statement properly?
IF(Field = "" or Field IS NULL,'b',concat(Field,'_b'))
You can use a CASE statement.
Example:
SELECT
CASE
WHEN myField IS NULL THEN 'b'
WHEN myField = '' THEN 'b'
ELSE myField + '_b'
END
FROM myTable
You need to use the CASE keyword.
Something like
UPDATE tbl
SET field =
CASE
WHEN field = ''
THEN 'b'
ELSE field + '_b'
END
You can use Cases but you can shorten it like this:
set Field = isnull(field, '')
+ case when isnull(field, '') = '' then '' else '_' end
+ 'b'
I have been struggling for quite some time to get this query going.
In short my query searches by fileno and/or searchfield
DECLARE #pSearchFor AS NVARCHAR(100);
-- I am here testing with null value, ' ' , or seperate words
SET #pSearchFor = null -- '"marsa" and "mosta"';
IF ISNULL(#pSearchFor,'') = '' SET #pSearchFor = '""' ;
declare #fileNo nvarchar(50) = 'e/e'
select top 1000 r.FileId, r.FileNo, fs.SearchField, #pSearchFor
from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like
CASE
WHEN Len(#fileno) > 1 THEN '%'+#fileNo+'%'
ELSE r.FileNo
END
AND
1 =
CASE WHEN ISNULL(#pSearchFor, '') = '' THEN 1 ELSE 0 END
or CONTAINS(fs.SearchField, #pSearchFor)
I am getting nothing returned if #pSearchFor is null otherwise works great.
I need to return all instances if a null
One possible solution might be to call 2 seperate sps or use if /else but probably exists a better method.
I really do appreciate your help!
First you set #pSearchFor to "":
IF ISNULL(#pSearchFor,'') = '' SET #pSearchFor = '""' ;
That means this will never return 1:
CASE WHEN ISNULL(#pSearchFor, '') = '' THEN 1 ELSE 0 END
You need to either use a different variable, or use the same type of CASE expression in the select list, instead of changing the value from NULL to "".
SELECT TOP 1000 r.FileId, r.FileNo, fs.SearchField,
CASE WHEN COALESCE(#pSearchFor, '') = '' THEN '""' ELSE #pSearchFor END
Also you use SELECT TOP but no ORDER BY ... if you want a subset, don't you care which subset you get?
I have solved the problem. Maybe this may be of some help to others!
This is a snippet of my stored procedure.
#fileNo nvarchar(50) = null ,
#fields nvarchar(100) = '""',`enter code here`
#datefrom date = null,
#dateto date = null,
...
AS`enter code here`
BEGIN
if (#fields = null or LEN(#fields) < 1 ) set #fields = '""'
select top 1000 r.*,
(CASE
WHEN fs.SearchField IS NULL THEN CONVERT(NVarChar(1),'')
ELSE CONVERT(NVarChar(MAX),fs.SearchField)
END) AS [Search]
from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like
CASE
WHEN Len(#fileno) > 1 THEN '%'+#fileNo+'%'
ELSE r.FileNo
END
and
r.Date between
CASE
WHEN #datefrom != '' THEN #datefrom
ELSE '1900-1-1'
END
and
CASE
WHEN #dateto != '' THEN #dateto
ELSE '9999-1-1'
END
and
((LEN(#fields) > 2 and contains(fs.SearchField,#fields))or (LEN(#fields) <= 2))
--NB: <= 2 as we have added the "" characters in #fields!
end
I have a procedure which receive a bit variable called #FL_FINALIZADA.
If it is null or false I want to restrict my select to show only the rows that contain null DT_FINALIZACAO values. Otherwise I want to show the rows containing not null DT_FINALIZACAO values.
Something like this:
SELECT
*
FROM
MyTable
WHERE
...
AND
(
OPE.DT_FINALIZACAO = (
CASE
WHEN (#FL_FINALIZADA <> 1)
THEN NULL
END
) OR
OPE.DT_FINALIZACAO IS NOT NULL
)
In this case I receive the message:
None of the result expressions in a
CASE specification can be NULL.
How can I achieve this?
Thanks in advance.
SELECT
*
FROM
MyTable
WHERE
(ISNULL(#FL_FINALIZADA, 0) = 0
AND
OPE.DT_FINALIZACAO IS NULL
)
OR
(#FL_FINALIZADA = 1
AND
OPE.DT_FINALIZACAO IS NOT NULL
)
Change the AND to be:
AND (((#FL_FINALIZADA <> 1) AND (OPE.DT_FINALIZACAO IS NULL)) OR ( (#FL_FINALIZADA = 1) AND (OPE.DT_FINALIZACAO IS NOT NULL)))
If the bit flag is 1 then DT_FINALIZACAO can't be null.
IF #FL_FINALIZADA IS NULL
SET #FL_FINALIZADA = 0
SELECT * FROM NewsletterSubscribers
WHERE
(#FL_FINALIZADA = 0 AND OPE.DT_FINALIZACAO IS NULL)
OR
(#FL_FINALIZADA = 1 AND OPE.DT_FINALIZACAO IS NOT NULL)
My detailed SQL is a little rusty, but have you tried using 0 insted of NULL? I would expect 0 to evaluate the same as NULL in that select