Avatar of SystemSys
SystemSysFlag for Saudi Arabia asked on

Urgent: How i can Use IF Statemant in SQL Server ??

hi all Experts ,,

i have this  sentence

SELECT Writer_code,[Member_Name], CAST(misc_col_flag as bit) as C1,CAST([all_misc_col_flag] as bit) as C2 FROM [ALWASAT_WRITERS] order by member_name


i want put if statement if the field misc_col_flag = null then set value for this field False
and Also same thing in all_misc_col_flag field if it = null then set for it value = false

i know statement in MS Access like this iif(

i want that in SQL Server 2005 Please

and thank you ,,

Microsoft SQL Server 2005DatabasesMicrosoft SQL Server

Avatar of undefined
Last Comment
SystemSys

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
TimCottee

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Patrick Matthews

SystemSys said:
>>i want that in SQL Server 2005 Please

In T-SQL use the CASE structure:

SELECT CASE SomeColumn WHEN 0 THEN 'a' WHEN 1 THEN 'b' ELSE 'c' END AS fubar
FROM SomeTable
TimCottee

SystemSys,

Whilst the IF construct is available in sql it is only used for control-of-flow syntax and cannot be used like the in-line if (Iif) function. You can as shown use the Case ...Else ... End construct in place of Iif though.

TimCottee
Aneesh

Use Case statement

CASE WHEN misc_col_flag  IS NULL THEN 0  ELSE misc_col_flag  END as misc_col_flag      ---assuming '0' for false
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
CMYScott

In your case, I think the CASE is more code than you need.  SQL has an ISNULL command that takes two params - the first is the value you want to check, the second is what to use IF the first value IS NULL.

For instance if I had a variable @TestValue which has a value of 5
ISNULL(@TestValue,99) - would result in 5 (because @TestValue is NOT NULL)
but.. if @TestValue was null
ISNULL(@TestValue,99) - would result in 99 (because @TestValue IS NULL)

there is also COALESCE - which accepts multiple values and returns the first one which is NOT null - in your case (the example below) - you could replace ISNULL with COALESCE and get exactly the same results.




SELECT Writer_code,
             [Member_Name],
             CAST(ISNULL(misc_col_flag,0) as bit) as C1,
             CAST(ISNULL([all_misc_col_flag],0) as bit) as C2
FROM [ALWASAT_WRITERS]
ORDER BY member_name
ASKER
SystemSys

GOOOOD JOB TimCottee YOU ARE THE BEST
ASKER
SystemSys

GOOOOOOOOOOOD JOB Experts
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.