Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Order By Statement, help please

Status
Not open for further replies.
Aug 22, 2011
13
US
Hi

I have the below order by statement

ORDER BY CASE WHEN [Last Name] IS NULL THEN CASE WHEN [First Name] IS NULL
THEN [Company] ELSE [First Name] END ELSE CASE WHEN [First Name] IS NULL THEN [Last Name] ELSE [Last Name] + ',' + [First Name] END END,
CASE WHEN [Last Name] IS NULL THEN CASE WHEN [First Name] IS NULL THEN [Company] ELSE [First Name] END CASE WHEN [First Name] IS NULL
THEN [Last Name] ELSE [First Name] + ',' + [Last Name] END END


I keep getting error "incorrect syntax near the keyword "CASE""

Can someone please coach me

thanks
 
Code:
CASE WHEN [Last Name] IS NULL 
     THEN 
          CASE WHEN [First Name] IS NULL
                        
               THEN [Company] ELSE [First Name] 
          END ELSE 
              CASE WHEN [First Name] IS NULL 
                   THEN [Last Name] ELSE [Last Name] + ',' + [First Name] 
              END 
END,
CASE WHEN [Last Name] IS NULL 
     THEN 
          CASE WHEN [First Name] IS NULL 
               THEN [Company]
               ELSE [First Name] 
          END  <--- missing one else here probably
          CASE WHEN [First Name] IS NULL
               THEN [Last Name] ELSE [First Name] + ',' + [Last Name] 
          END 
END


Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Just a small tip, when working with nested CASE's, add parentheses to make it easier to read and understand.

Also, the SQL Validator can be useful to find syntax errors:
(But then you'll have to use ANSI style [tt]"First Name"[/tt] instead of [tt][First Name][/tt], and [tt]||[/tt] instead of [tt]+[/tt].)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top