Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 check for null values

Author  Topic 

jassie
Constraint Violating Yak Guru

332 Posts

Posted - 2012-10-18 : 01:41:48
In a sql server 2008 r2 database, I have a column that can be null. What I am looking for values that equal zero. The column I am looking at is declared as an ineger.

Thus how do I check for the value = 0, but not select recrods where the value in this column can be null.

How would I do this check in a where statement?

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-10-18 : 01:54:56


SELECT *
FROM YourTableName
WHERE ColumnName = 0


--
Chandu
Go to Top of Page

Andy Hyslop
Starting Member

14 Posts

Posted - 2012-10-18 : 04:35:14
Or is you just wanted to return any integers excluding NULLS without hard coding the WHERE


DECLARE @TABLE AS TABLE
(NUMBER INT)

INSERT INTO @TABLE
(NUMBER)

SELECT NULL UNION ALL
SELECT NULL UNION ALL
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 2

SELECT *
FROM @TABLE
WHERE
ISNUMERIC(NUMBER) = 1



Andy
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-10-18 : 05:34:30
copy/paste this into a query window, run it and see what happens.

declare @sample table (id int null)
insert @sample
select null union all
select 5 union all
select 10 union all
select 0 union all
select null

select * from @sample
where id=0

select * from @sample
where id is null

select * from @sample
where id is not null



Too old to Rock'n'Roll too young to die.
Go to Top of Page
   

- Advertisement -