SQL Clone
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
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 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
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63065 Visits: 17959
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 Modens 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
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

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

Group: General Forum Members
Points: 34 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 Guru
SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)

Group: General Forum Members
Points: 226361 Visits: 46325
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
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 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