title: SQL select case statement
body: <p>i am using SQL Server 2008 R2
  i have the store procedure like this:</p>
  
  <pre><code>CREATE PROCEDURE Get_Code_Tourne_Matin
      -- Add the parameters for the stored procedure here
  @The_FA int,
  @The_Jour int,
  @The_Insee varchar(10)
  AS
  BEGIN
  declare @TheCode varchar(250)
  
  
  set @TheCode = case
      when exists (SELECT T_TOURNE_LABEL.LIBELLE 
                    FROM TOURNE
                    LEFT JOIN T_TOURNE_LABEL ON TOURNE.LIB_TOURNE = T_TOURNE_LABEL.NOID
                    WHERE THE_FA = @The_FA AND 
                          NO_INSEE = @The_Insee AND 
                          JOUR = @The_Jour AND 
                         (datepart(hh, LE_HEURE) between 13 and 23 or datepart(hh, LE_HEURE) between 0 and 6)) then LIBELLE
      when exists (SELECT LIBELLE
                    FROM TOURNE
                    LEFT JOIN T_TOURNE_LABEL ON TOURNE.LIB_TOURNE = T_TOURNE_LABEL.NOID
                    WHERE THE_FA = @The_FA AND 
                          NO_INSEE = @The_Insee AND 
                          JOUR = 0 AND 
                         (datepart(hh, LE_HEURE) between 13 and 23 or datepart(hh, LE_HEURE) between 0 and 6)) then LIBELLE
      else '00'
  
  
      end
  
  
  RETURN @TheCode  
  END
  </code></pre>
  
  <p>i just want to return the libelle.<br>
  the first select with 3 variable when it found then retrun the libelle<br>
  else if run select with 2 variable when it found then retrun the libelle <br>
  else retrun '00'</p>
  
  <p>it seems syntax error on LIBELLE</p>
  
  <p>any idea ?</p>