October 2, 2008 at 10:00 am
Need to check if the field has space and null
if (select column from table) not (null and ' ')
print 'value exists'
how to write IF condition for this??
I tried like
if (select column from table) in ('','null')
print 'nul'
else
print 'not null'
but it could not check null condition
it prints not null
October 2, 2008 at 10:26 am
Are you after a query or funciton
for a query
select COLUMN from TABLE where COLUMN is null
Let me know what you are trying to acheive and i can offer further help
October 2, 2008 at 10:51 am
if exists (select * from table where nullif(column, ' ') is null)
print 'column contains either a null or a space'
else
print 'column contains neither a null nor a space'
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
October 2, 2008 at 11:26 am
If (select isnull(column, '') from table) != ''
print 'Not Null or Empty'
Else
print 'Null or Empty'
I'm using Empty for just a space.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
October 2, 2008 at 11:33 am
GSquared (10/2/2008)
If (select isnull(column, '') from table) != ''
print 'Not Null or Empty'
Else
print 'Null or Empty'
I'm using Empty for just a space.
What if the table has no rows?
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
October 2, 2008 at 12:04 pm
declare @var varchar (20)
set @var = (SELECT distinct column from table where id = 1234 )
select @var
IF (@var is null) or (@var = '')
PRINT 'space'
else
print 'no space'
October 2, 2008 at 12:43 pm
October 3, 2008 at 2:13 pm
RyanRandall (10/2/2008)
GSquared (10/2/2008)
If (select isnull(column, '') from table) != ''
print 'Not Null or Empty'
Else
print 'Null or Empty'
I'm using Empty for just a space.
What if the table has no rows?
I have to admit, I'm operating on the assumption that the inline query has more to it than the sample I put together.
Also, if there are no rows, it will print Null or Empty, which might be valid in this case, or might not, depending on the business-need for the query.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
October 3, 2008 at 5:52 pm
It doesn't need to be that complicated if you understand that NULL is NOT nothing and cannot be compared to using relational operators...
IF (SELECT somecolumn FROM sometable) > ' '
PRINT 'Not NULL and Not BLANK'
ELSE
PRINT 'IS NULL or BLANK'
You'll also find that it runs just a wee bit faster if you have a very large data set...
Of course, none of the code in this format really solves the OP's original question unless we add a WHERE clause with the proper criteria... if the goal is to check the whole bloody table, something like the following might be more appropriate...
IF EXISTS (SELECT 1 FROM sometable WHERE somecolumn > ' ')
PRINT 'Not NULL and Not BLANK'
ELSE
PRINT 'IS NULL or BLANK'
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply