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 2000 Forums
 SQL Server Development (2000)
 IF Statement

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-13 : 18:24:40
majnoon writes "Can I put an IF statement in the select part of a query?

I have a column selected that contains nulls and I want to set that column to another value where it is null

can I do it using the following type of query

SELECT (IF Offences IS NULL BEGIN 'NO OFFENCES' END)
From Criminal_Record"

JamesT
Yak Posting Veteran

97 Posts

Posted - 2002-03-13 : 18:33:14
You can certainly use the case statement. I have never gotten IF to work in select although I have not tried that hard. Sample

select gender = case when gender = 'M' then 'MALE' when gender = 'F' then 'FEMALE' else 'UNKNOWN' end
from employees

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-03-13 : 18:42:55
IF can't work in a SELECT statement. IF in SQL Server operates like the IF...THEN statement in Visual Basic, NOT like the IIF() function. CASE works like the IIF() function.

Go to Top of Page

joldham
Wiseass Yak Posting Master

300 Posts

Posted - 2002-03-13 : 23:27:14
Maybe I am missing something, but if you are only checking for Null values then why not use:

SELECT IsNull(Offences, 'NO OFFENCES')
From Criminal_Record

Jeremy

Go to Top of Page
   

- Advertisement -