title: SQL Server 2008 - error 241
body: <p>I have an error in my stored procedure on SQL Server 2008.</p>
  
  <p>I need to do stuff and insert data in my table only if the current element doesn't already exist.</p>
  
  <p>So I create a stored procedure:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[upload_bank_file] 
      @date varchar(10), 
      @date_valeur varchar(10), 
      @libelle varchar(255), 
      @mode varchar(10), 
      @code varchar(10), 
      @date_effet varchar(10), 
      @debit float, 
      @credit float
  AS
  BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;
  
  print @credit;
  IF Not EXISTS (SELECT 1 FROM dbo.bank_import WHERE dbo.bank_import.Libelle = @libelle)
      --DROP PROCEDURE upload_bank_file; 
  Begin 
      IF (SUBSTRING(@date_effet, 4, 2) LIKE '01' AND SUBSTRING(@date, 1, 2) LIKE '12')
          SET @date_effet = CONVERT(varchar, (CONVERT(int, SUBSTRING(@date, 7, 2)) + 1)) + '-' + @date_effet;
      ELSE
          SET @date_effet = SUBSTRING(@date, 7, 2) + '-' + @date_effet;
  
      insert into dbo.bank_import ([Date], [Date_Valeur], [Libelle], [Mode], [Code], [Date_Effet], [Debit], [Credit])
      VALUES (CONVERT(datetime, @date, 120), CONVERT(datetime, @date_valeur, 120), @libelle, @mode, @code, CONVERT(datetime, @date_effet, 120), @debit, @credit);
      END
  END
  </code></pre>
  
  <p>When I test it via my php moulinette (to automatize file parsing and call to the stored procedure), I got the following error :</p>
  
  <blockquote>
    <p>[0] => 22007<br>
    [SQLSTATE] => 22007<br>
    [1] => 241<br>
    [code] => 241<br>
    [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Échec de la conversion de la date et/ou de l'heure à partir d'une chaîne de caractères.<br>
    [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Échec de la conversion de la date et/ou de l'heure à partir d'une chaîne de caractères.  </p>
  </blockquote>
  
  <p>I'm passing date to the stored procedure via <code>varchar</code>, in <code>yyyy/mm/dd</code> format, and <code>mm/dd</code> format for <code>@date</code> parameter.</p>
  
  <p>The SQL Server Management Studio, when I do </p>
  
  <pre><code>EXEC upload_bank_file 'with', 'fake', 'parameters' ...
  </code></pre>
  
  <p>is locating the error on the line 20 of my stored procedure, which is the line where I do my "If Exists ( Select ... )", only when my libelle didn't exists in the table.</p>
  
  <p>I googled this error several times, but i have found nothing useful. I passed my date on <code>yyyy/mm/dd</code> format and I have specify the <code>SET DATEFORMAT ymd</code> flag to my stored procedure, but my problem remains.</p>
  
  <p>If someone have any idea to help me, I will be grateful!</p>
  
  <p>(English is not my mother tongue, please excuse me for the errors ...)</p>
  
  <p>Thanks !</p>