Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server 2012 IF THEN logic to CASE THEN


SQL Server 2012 IF THEN logic to CASE THEN

Author
Message
jjgier
jjgier
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 38
Hey Everyone,

I have a set of IF THEN logic that create additional fields. Does anyone have a great grasp of how to convert the attached examples from IF THEN to CASE THEN statements. Any help would be appreciated.

Example 1:
IIF(fmwa.GroupNbr<>'','NO', IIF(dind.DefaultIndicator='Y', 'YES', 'NO')) AS Default_ID

Example 2:
IIF(tagagt.AgencyName='',(RTrim(tagagt.LastName)) + N', ' + LTrim(RTrim(tagagt.FirstName)), LTrim(RTrim(tagagt.AgencyName))) AS WA_Name
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16539 Visits: 16992
jjgier (5/14/2013)
Hey Everyone,

I have a set of IF THEN logic that create additional fields. Does anyone have a great grasp of how to convert the attached examples from IF THEN to CASE THEN statements. Any help would be appreciated.

Example 1:
IIF(fmwa.GroupNbr<>'','NO', IIF(dind.DefaultIndicator='Y', 'YES', 'NO')) AS Default_ID

Example 2:
IIF(tagagt.AgencyName='',(RTrim(tagagt.LastName)) + N', ' + LTrim(RTrim(tagagt.FirstName)), LTrim(RTrim(tagagt.AgencyName))) AS WA_Name


Like this?

Example 1:

case when fmwa.GroupNbr <> '' then 'NO' when dind.DefaultIndicator = 'Y' then ' YES' else 'NO' end as Default_ID

Example 2:

case when tagagt.AgencyName = ''
   then RTrim(tagagt.LastName)) + N', ' + LTrim(RTrim(tagagt.FirstName)
   else LTrim(RTrim(tagagt.AgencyName))
end as WA_Name

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
jjgier
jjgier
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 38
Thank you for your quick response. I will have to try that out.
jjgier
jjgier
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 38
Tested that out last night, it worked great! But another question, how do I place the Case Statements together? I tried this last night and it didn't work out.

CASE
CASE WHEN fmwa.GroupNbr<>'' THEN 'NO'
WHEN dind.DefaultIndicator='Y' THEN 'YES'
ELSE 'NO'
END AS Default_ID,

CASE WHEN tagagt.AgencyName=''
THEN RTrim(LTrim(tagagt.LastName)) + N', ' + RTrim(LTrim(tagagt.FirstName))
ELSE RTrim(LTrim(tagagt.AgencyName))
END AS WA_Name
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47173 Visits: 44350
That would be correct as part of a SELECT statement. CASE isn't a statement, it's an expression used within a query.

So

SELECT
CASE WHEN fmwa.GroupNbr<>'' THEN 'NO'
WHEN dind.DefaultIndicator='Y' THEN 'YES'
ELSE 'NO'
END AS Default_ID,
CASE WHEN tagagt.AgencyName=''
THEN RTrim(LTrim(tagagt.LastName)) + N', ' + RTrim(LTrim(tagagt.FirstName))
ELSE RTrim(LTrim(tagagt.AgencyName))
END AS WA_Name
FROM <some table> ....




Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


jjgier
jjgier
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 38
Thanks for all the help guys! :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search