title: Nested Case statement in sql query
body: <p>My Query </p>
  
  <pre><code>Declare     @From       DateTime='01 Feb 2013'
  Declare     @To         DateTime='28 Feb 2013 23:59:59'
  
  Select 
              Case
                    When
                    (
                          Select TOP 1 f1.UpdatedOn
                          From TicketTypeFollowUp As f1 with(nolock)
                          Where f1.UpdatedOn&lt;T.UpdatedOn And f1.Ticket=T.Ticket
                          Order By f1.UpdatedOn Desc
                    )IS Null
                    Then Ticket.TicketRaisedOn
  
                    Else
                    (
                         Select TOP 1 f1.UpdatedOn
                         From TicketTypeFollowUp As f1 with(nolock)
                         Where f1.UpdatedOn&lt;T.UpdatedOn And f1.Ticket=T.Ticket
                         Order By f1.UpdatedOn Desc
                    ) End [Start Date]
  
        From dbo.TicketTypeFollowUp T with(nolock)
        --Some Tables Omitted          
        Where CAST(TicketRaisedOn As Date)Between ''+Convert(VarChar(19), @From, 100)+''       And ''+Convert(VarChar(19), @To, 100)+''
  </code></pre>
  
  <p><strong>If the Column <code>[Start Date]</code> has values Greater than <code>@To</code> Then <code>@To</code> Value should print Else [Start Date] Column</strong></p>
  
  <p>How can i write it in the same query ?</p>
  
  <p>What i tried</p>
  
  <pre><code>       Case When(
               Case
                    When
                    (
                          Select TOP 1 f1.UpdatedOn
                          From TicketTypeFollowUp As f1 with(nolock)
                          Where f1.UpdatedOn&lt;T.UpdatedOn And f1.Ticket=T.Ticket
                          Order By f1.UpdatedOn Desc
                    )IS Null
                    Then Ticket.TicketRaisedOn
  
                    Else
                    (
                         Select TOP 1 f1.UpdatedOn
                         From TicketTypeFollowUp As f1 with(nolock)
                         Where f1.UpdatedOn&lt;T.UpdatedOn And f1.Ticket=T.Ticket
                         Order By f1.UpdatedOn Desc
                    ) End [Start Date]
                )&gt; @To Then @To Else [Start Date] End,
  </code></pre>
  
  <p>DB : SQL SERVER 2008</p>