SQL Server - IF statement - Asked By jason barry on 20-Nov-12 01:19 AM

Hi, I am trying to write an IF statement which will perform the following.....

Here is an example of the results in the table....

Name        Exd Name                 RESULT
HMAA  USA       USA                  HMAA USA
HMIT  ITL       ITL                  HMIT ITL
HMTT   LOG       HYT                HMTT LOG (HYT)
LTYS   LTT      FYM                  LTYS LTT (FYM)
TTTT   FFF      PPP                 TTTT FFF (PPP)
FDSE   FGR      FGR                 FDSE FGR

The results should be displayed as follows

HMTT LOG (HYT) -----as the two names are different I wan to display the Exd NAme in brackets), however when
HMAA USA , and the Exd Name is also USA - I only want to display the 'Name', not the Exd Name.

Is this possible in a IF statement....This is my script, as you can see it has the name + Exd Name, however above I would like to incorporate an IIF statement in here to only display as per the above logic.








.[name] + ' ' + '(' + rnl.[Exd Name] + ')' as 'Vessel'


resultsIB RNL (nolock)

MARKAND BHATT replied to jason barry on 20-Nov-12 09:11 AM
Try this query

1.SELECT  distinct  RNL.EmplID, RNL.ServiceType, 
2.case when rnl.[name] like '%'+rnl.[Exd Name]+'%' then
3.rnl.[name] else
4.rnl.[name] + ' ' + '(' + rnl.[Exd Name] + ')'  end as 'Vessel'
6.resultsIB RNL (nolock)