Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for null in if statement

Status
Not open for further replies.

crystaldev1

Programmer
Joined
Nov 6, 2003
Messages
232
Location
US
Hello. I am using Sql Server 2005 and would like to run the following statement but it's giving me few errors:

SELECT ...

FROM ...

WHERE (if TABLE.FIELD IS NOT NULL then TABLE.bPRIMARY = 'True'
else '')

So I'm having a problem with the where statement. I would like to use the IS NULL or IS NOT NULL in IF statement if possible. Thanks.
 
IF is used to control the flow of logic. It's really meant for multiple queries. Ex: If row exists in table, update, otherwise insert new row.

To control DATA, you'll want to use the Case/When construct, like this:

[tt][blue]
WHERE Table.bPrimary = Case When TABLE.FIELD IS NULL
Then ''
Else 'True'
End
[/blue][/tt]

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top