Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Server 2012 IF THEN logic to CASE THEN Expand / Collapse
Author
Message
Posted Tuesday, May 14, 2013 9:47 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 1:50 PM
Points: 8, Visits: 33
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
Post #1452688
Posted Tuesday, May 14, 2013 10:00 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:34 PM
Points: 13,481, Visits: 12,342
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)
Post #1452694
Posted Tuesday, May 14, 2013 12:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 1:50 PM
Points: 8, Visits: 33
Thank you for your quick response. I will have to try that out.
Post #1452766
Posted Wednesday, May 15, 2013 7:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 1:50 PM
Points: 8, Visits: 33
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
Post #1453083
Posted Wednesday, May 15, 2013 7:35 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:43 PM
Points: 43,047, Visits: 36,206
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 2008, MVP
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

Post #1453092
Posted Wednesday, May 15, 2013 8:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 1:50 PM
Points: 8, Visits: 33
Thanks for all the help guys!
Post #1453149
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse